7 JavaScript Differences Between Firefox & IE
Please note that this is an older article that discusses JavaScript issues in Internet Explorer versions 6 & 7, along with Firefox versions 2+ & 3.0.
Although the days of long and tedious code branches to target specific browsers in JavaScript are over, once in a while it’s still necessary to do some simple code branching and object detection to ensure that a certain piece of code is working properly on a user’s machine.
In this article, I’ll outine 7 areas where Internet Explorer and Firefox differ in JavaScript syntax.
1. The CSS “float” property
The basic syntax for accessing a specific css property for any given object is
object.style.property
, using camel casing to replace a hyphenated property. For example, to access the background-color
property of a <div>
whose ID is “header”, we would use the following syntax:- document.getElementById("header").style.backgroundColor= "#ccc";
But since the word “float” is already reserved for use in JavaScript, we cannot access the “float” property using
object.style.float
. Here is how we do it in the two browsers:The IE Syntax:
- document.getElementById("header").style.styleFloat = "left";
The Firefox Syntax:
- document.getElementById("header").style.cssFloat = "left";
2. The Computed Style of an Element
JavaScript can easily access and modify CSS styles that have been set on objects using the
object.style.property
syntax outlined above. But the limitation of that syntax is that it can only retrieve styles that have been set inline in the HTML or styles that have been set directly by JavaScript. The style object does not access styles set using an external stylesheet. In order to access an object’s “computed style”, we use the following code:The IE Syntax:
- var myObject = document.getElementById("header");
- var myStyle = myObject.currentStyle.backgroundColor;
The Firefox Syntax:
- var myObject = document.getElementById("header");
- var myComputedStyle = document.defaultView.getComputedStyle(myObject, null);
- var myStyle = myComputedStyle.backgroundColor;
3. Accessing the “class” Attribute of an Element
As is the case with the “float” property, our two major browsers use different syntax to access this attribute via the
getAttribute
method in JavaScript.The IE Syntax:
- var myObject = document.getElementById("header");
- var myAttribute = myObject.getAttribute("className");
The Firefox Syntax:
- var myObject = document.getElementById("header");
- var myAttribute = myObject.getAttribute("class");
This syntax would also apply using the
setAttribute
method.4. Accessing the “for” Attribute of the <label> Tag
Similar to number 3, we have different syntax to access a <label> tag’s “for” attribute in JavaScript.
The IE Syntax:
- var myObject = document.getElementById("myLabel");
- var myAttribute = myObject.getAttribute("htmlFor");
The Firefox Syntax:
- var myObject = document.getElementById("myLabel");
- var myAttribute = myObject.getAttribute("for");
5. Getting the Cursor Position
It would be rare that you would want to find the cursor position of an element, but if for some reason you need to, the syntax is different in IE and Firefox. The code samples here are fairly basic, and normally would be part of a much more complex event handler, but they serve to illustrate the difference. Also, it should be noted that the result in IE will be different than that of Firefox, so this method is buggy. Usually, the difference can be compensated for by getting the “scrolling position” — but that’s a subject for another post!
The IE Syntax:
- var myCursorPosition = [0, 0];
- myCursorPosition[0] = event.clientX;
- myCursorPosition[1] = event.clientY;
The Firefox Syntax:
- var myCursorPosition = [0, 0];
- myCursorPosition[0] = event.pageX;
- myCursorPosition[1] = event.pageY;
6. Getting the Size of the Viewport, or Browser Window
Sometimes it’s necessary to find out the size of the browser’s available window space, usually called the “viewport”.
The IE Syntax:
- var myBrowserSize = [0, 0];
- myBrowserSize[0] = document.documentElement.clientWidth;
- myBrowserSize[1] = document.documentElement.clientHeight;
The Firefox Syntax:
- var myBrowserSize = [0, 0];
- myBrowserSize[0] = window.innerWidth;
- myBrowserSize[1] = window.innerHeight;
7. Alpha Transparency
Okay, this is not a JavaScript syntax issue — alpha transparency is set via CSS. But when objects fade in and out via JavaScript, this is done by accessing CSS alpha settings, usually inside of a loop. The CSS code that needs to be altered via JavaScript is as follows:
The IE Syntax:
- #myElement {
- filter: alpha(opacity=50);
- }
The Firefox Syntax:
- #myElement {
- opacity: 0.5;
- }
So, to access those values via JavaScript, we would use the style object:
The IE Syntax:
- var myObject = document.getElementById("myElement");
- myObject.style.filter = "alpha(opacity=80)";
The Firefox Syntax:
- var myObject = document.getElementById("myElement");
- myObject.style.opacity = "0.5";
Of course, as mentioned, normally the opacity/alpha would be changed in the midst of a loop, to create the animating effect, but this simple example clearly illustrates how it’s done.
Are There Other Differences?
These are some of the differences I’ve personally come across either in development or research, but I’m sure there are others. Leave a comment if you think I’ve left out an important one, and I’ll try to update this list, or else do a “Part 2″ post.
No comments:
Post a Comment