Blog of Adrian Anttila, containing my thoughts, comments and questions. RSS Feed


JavaScript differences in Firefox and Internet Explorer

While working on the Vertigo.com redesign, I had the opportunity to work with some of the new ASP.NET AJAX controls.  We decided to use a richer interface than static HTML, which gave me the opportunity (read: frustration) to support Firefox and Internet Explorer clients. 

Over the last several years, most of my web development has been to support corporate intranet applications, where the browser was Internet Explorer.  As I got to know more and more JavaScript and DHTML, I tried to keep cross-browser support in mind, even though it wasn't a requirement.  The vertigo.com project showed me that despite my best efforts to keep my JavaScript agnostic, I still had a lot to learn.  I'd like to list some of the things I learned here.

Checking for browser version

In all my examples, I will be checking for browser version by checking to see if the document.all property is available, which is only available in Internet Explorer.

Window/Body OnLoad

When dealing with a site that uses Master Pages, you lose some control, including the ability to declare a page specific event handler. There are ways around this, and I found that the following code example works pretty well.

<script language="javascript" type="text/javascript" for="window" event="onload">
    if
(document.all) { initialize(); }
    else { window.document.addEventListener("DOMContentLoaded", initialize, true); }
</script>

In this case, I wrote a function on the inheriting page called initialize, which served as a typical <body onload="initialize()"> call would perform. In Internet Explorer, the onload event for the window object works exactly like the onload event for the body object, so initialize can be called as-is. However, the behavior is different in Firefox, and you have to add an event listener to the DOMContentLoaded event instead of making a direct call since the window.onload event fires before the window content is rendered and the DOM is available.

Element Height and Width

To retrieve an element's height and width, use element.currentStyle for Internet Explorer and element.style for Firefox.

if (document.all)
{
    top += parseValue(element.currentStyle.borderTopWidth);
    ...
}
else

    top += parseValue(element.style.borderTopWidth); 
    ...
}

Text Content

Retrieving the text of an element is done by using element.innerText in IE and element.textContent in Firefox.

function getText(control)

    if (document.all) 
    { 
        return control.innerText; 
    } 
    else 
    { 
        return control.textContent; 
    }
}

function setText(control, value)

    if (document.all) 
    { 
        control.innerText = value; 
    } 
    else 
    { 
        control.textContent = value; 
    }
}

Source Elements

To get a reference to the element that fired an event within an event handler, use event.srcElement for IE and event.target for Firefox.

function getSourceElement(event)

    if (document.all) 
    { 
        return event.srcElement; 
    } 
    else 
    { 
        return event.target; 
    }
}

Firing Events

You can call fireEvent on an element in IE, but it's more complicated in Firefox, requiring an event to be instantiated, initialized, and dispatched.

function fireClickEvent(control)

    if (document.all) 
    { 
        control.fireEvent("onclick"); 
    } 
    else 
    { 
        var clickEvent = window.document.createEvent("MouseEvent"); 
        clickEvent.initEvent("click", false, true); 
        control.dispatchEvent(clickEvent); 
    }
}

Window Height and Width

IE exposes the visible content area dimensions using document.body.offsetHeight and document.body.offsetWidth, whereas Firefox uses document.body.offsetWidth and window.innerWidth.

function getWindowHeight()
{
    if (document.all)
    {
        return document.body.offsetHeight; 
    } 
    else 
    { 
        return window.innerHeight; 
    }
}

function getWindowWidth()

    if (document.all) 
    { 
        return document.body.offsetWidth; 
    } 
    else 
    { 
        return window.innerWidth; 
    }
}

Figuring all this out took more time that I would have liked, and I hope sharing it will help others. I'm glad to get any feedback on improving these methods, and would also love to hear if anyone has anything else they'd like to share.

 
Posted by Adrian Anttila | 9 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Friday, 23 Mar 2007 04:45 by Thx
Quite useful. I needed textContent equivalent in IE and found it here (:

Monday, 22 Oct 2007 01:27 by Thanks.
bonus: ie | firefox fromElement | relatedTarget toElement | currentTarget returnValue | preventDefault() cancelBubble | stopPropogation() attachEvent(event,func) | addEventListener(event,func,usecapture) detachEvent(event,func) | removeEventListener(event,func,usecapture) Deniz Dursun bidost [at} gmail.com

Thursday, 17 Jan 2008 09:33 by Re: JavaScript differences in Firefox and Internet Explorer
Wow! I found this blog entry, while searching for innerText. And thought this entry was some years old. BUT WAIT its from 2007?!? You explain this, like it were Firefox which does it more complicated and 'wrong'. But that's not true. He does it in the standards-way, like other browsers do. IE is years behind Firefox, Opera and Safari! You should throw some of your knowledge away. Really!

Friday, 15 Feb 2008 09:12 by Note
Remark on Deniz Dursun's comment, The following isn't quite true: ie | firefox fromElement | relatedTarget toElement | currentTarget Here is what the W3C says, - about the attribute 'currentTarget': "Used to indicate the EventTarget whose EventListeners are currently being processed." - about the attribute 'relatedTarget': "it is used with the mouseover event to indicate the EventTarget which the pointing device exited and with the mouseout event to indicate the EventTarget which the pointing device entered" This is different from IE's version which acts regardless of what event (mouseover or mouseout) is triggered. According to the MSDN: - about the attribute 'fromElement': "retrieves the object from which activation or the mouse pointer is exiting during the event." - about the attribute 'toElement': "retrieves a reference to the object toward which the user is moving the mouse pointer." I needed to clear this out as I spent a few hours wondering why my code was not working in Firefox after reading this comment. Daniel

Thursday, 21 Feb 2008 03:44 by Re: JavaScript differences in Firefox and Internet Explorer
"You should throw some of your knowledge away. Really!" I think that wouldn't make much sense, given that IE is still the dominant browser. Yes, Safari and FireFox are gaining considerable ground, but coming from a intranet (i.e. corporate) developer background, you should recognize that a lot of "web developers" are used to building applications that specifically target IE. As as for my statements of FireFox being "wrong", I'll stand by my statements as they are posted. Coding against FireFox can be more complicated, and it's much harder to find good documentation as to how things work (reading the W3C standard document isn't appealing, nor is it a good developer reference).

Friday, 22 Feb 2008 05:05 by Re: JavaScript differences in Firefox and Internet Explorer
"And thought this entry was some years old. BUT WAIT its from 2007?!? " I have the "Dynamic HTML" from Danny Goodman from July 1998 (ISBN 9 781565 924949), so almost 10 years old. It describes the innerHTML and inner HTML functions on page 466. The outerHTML and outerTEXT functions are described on page 469. I recently tried to use the outerHTML function to make a menu dynamic. You define your menu only once and in any web-page from your website you make a reference to that one specific menu. If ever your menu changes, you only have to do it once. With IE it works exactly as I hoped, in Firefox it doesn't. Does anyone have experience with the outerHTML function within Firefox?

Wednesday, 27 Feb 2008 01:33 by Re: JavaScript differences in Firefox and Internet Explorer
Great it worked for me

Monday, 17 Mar 2008 03:54 by Re: Re: JavaScript differences in Firefox and Internet Explorer
"Coding against FireFox can be more complicated, and it's much harder to find good documentation as to how things work" Have you ever seen Firebug and http://developer.mozilla.org/ ?

Friday, 29 Aug 2008 11:45 by ADAC
if (document.all) { initialize(); } else { window.document.addEventListener("DOMContentLoaded", initialize, true); } Very Useful Thanks

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation