window.attachEvent and FireFox

- tagged ,

After reading Rumen Stankov's entry on telerik controls running on ie7 it reminded me of an interesting feature of Atlas. He brought up the Object detection method of deciphering what Browser is running your script.

// Good - the Object Detection Way. This is how it should be done.
if (window.attachEvent)
{
window.attachEvent('onunload', dispose);
}

// Bad. This is the most common IE client-side detection though.
if (document.all)
{
window.attachEvent('onunload', dispose);
}

What's interesting is, that if Atlas detects that you are running browsers other than IE it will include an additional file for compatibility. This .js file than adds a function to the window object called attachEvent which than wraps addEventListener.

This is great because now to attach events all you need to do is use the attachEvent function. But if you are using Atlas with your site, window.attachEvent is no longer IE only and therefore not useful as a function to be checked against for general browser detection. Something to think about, especially if you are planning on selling web controls.

posted on Feb 16th, 2006 | Permalink | Comments (7)

7 Comments »

  1. Technically it's Firefox, not FireFox.

    FWIW I absolutely agree that

    if (foo) {
    // something that uses foo
    }

    is far superior to

    if (the moon is full and it's Tuesday)
    {
    // something that uses foo
    }

    ... even if, so far, every time "foo" has been available it's been a full moon on Tuesday.

    Comment by Maurits - February 17, 2006 @ 5:31 PM
  2. I strongly disagree with the approach adopted by Atlas. One should really write cross browser code(as noted by Rumen Stankov) rather than extend W3C DOM objects with Microsoft's proprietary API(attachEvent). Many public domain JavaScript libraries will just stop working when used along with Atlas. And what about the other IE "peculiarities"? Should other browsers emulate them too? If yes - how? I wonder why Microsoft always tries to reinvent the wheel when it comes to software development. First it tries to redefine what test driven development means (yes, the article has been deleted from MSDN) now with Atlas it tells us how to write cross-browser JavaScript. Unfortunately Atlas, being a Microsoft creation, will prevail and we shall follow its "best practices". A rather good read on cross-browser JavaScript is www.quirksmode.org.

    Comment by A humble JavaScript developer - February 18, 2006 @ 7:27 AM
  3. I'm with you: http://weblogs.asp.net/jgalloway/archive/2005/08/07/421798.aspx

    Comment by Jon Galloway - March 07, 2006 @ 11:29 PM
  4. The best Javascript library I've seen for handling events in a cross browser manner is the Dojo toolkit. 2nd best is the new Yahoo UI library. Dojo wins because it isn't as DOM centric and the YUI (It allows you to wire up events between two javascript objects rather than just DOM objects).

    Comment by Scott - March 08, 2006 @ 7:28 AM
  5. I'm not sure we need a full library of code to condition for only window.attachEvent. I too like a complete solution, but sometimes it is overkill. The atlas method is fine. But yogesh 's points must be taken into consideration...poignant yogesh, absolutely poignant.

    A

    Comment by Aaron - June 01, 2007 @ 11:13 AM
  6. I believe Aaron is right, point by yogest must be takin into consideration

    Comment by naresh kumar - September 13, 2007 @ 10:16 PM
  7. Agreed, Atlas has changed to ASP.Net Ajax and now advocates an addHandler method instead.

    Comment by Adam Kinney - September 19, 2007 @ 5:14 PM

Leave a comment