Wednesday, September 28, 2005

The Ghost of NS4

There are times when you simply have to throw in the towel and do something evil. Such as, oh, make Firefox behave like Netscape 4.

A widely-used utility script was causing fatal errors in Firefox. These errors were halting the JS interpreter completely, which shut down my own code before it could even be executed. Closer examination revealed that the utility script jammed all browsers into the IE4 or NS4 category, and made extensive use of NS4-only properties like document.layers, layer.clip and layer.pageX.

Fixing the utility script to work with Firefox was not an option. The script is deployed to literally hundreds of thousands of existing websites as part of a popular WYSIWYG program. Even if I was able to provide a patch to the script, there would be no way to compel people to upgrade.

Not to mention that I'd be the one stuck maintaining and supporting a script that I didn't write.

Worse, browser detection was buried deep inside the utility script and done on the fly in several spots, all with locally-scoped variables that were impossible to overwrite.

I had to find a way to unblock these errors without touching the script.

So:

document.layers = document.getElementsByTagName("*");

Unngh! It hurts! Make it stop!

HTMLElement.prototype.pageX getter = function () { return this.offsetLeft; };
HTMLElement.prototype.pageY getter = function () { return this.offsetTop; };

Please kill me now.

I'll leave it to you to figure out how I took care of things like the getting and setting of properties like clip.left, clip.top, clip.right, etc. (hint: it involves the use of getComputedStyle, parseInt, a number of string methods and IT AIN'T PRETTY, THAT'S FOR SURE.)

Oh wait, IE6 chokes on this and tells me it's expecting a semicolon. Well, of course. IE doesn't support getter/setter. So I have to wrap this whole thing in a server-side UA sniff to ensure that it's never delivered to IE.

The ghost of NS4 continues to haunt, its icy incorporeal fingers reaching out to mess with my head.

Monday, September 26, 2005

Del.icio.us with Blogspot blogs?

I thought I'd be able to use del.icio.us links in conjunction with this blog (not as a sidebar, but the handy daily automated post feature), but it's been a long while since I used the Blogger API, and there doesn't seem to be a target URI for posting that I know of. Anyone out there know how I can get this working? Or am I just hosed?

Thursday, September 08, 2005

addEvent() recoding contest

Peter-Paul Koch has put together a contest to recreate my addEvent() and removeEvent() utilities in a way that compensates for its inherent shortcomings:

Write your own version of addEvent() and removeEvent(), submit it by adding a comment to this page, and win JavaScript fame.

Entries will be judged by a panel consisting of Scott Andrew LePera, Dean Edwards and myself on script quality, simplicity and portability, and the clarity of your explanation.

I'll also update the original tutorial that introduced addEvent() with a link to the winning entry.

Previously: Know your (event) sources.

Sunday, September 04, 2005

A treasure trove of quirks

If you're not already reading Hallvord R. M. Steen's Miscoded Blog, you probably should be. Hallvord's a JavaScript QA tester for Opera Software, and uses his blog to record many of the JS and browser quirks and caveats he comes across. Some sample posts:

Good stuff. Read it.