7 JavaScript Differences Between Firefox & IE

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. In this article, I'll outine 7 areas where Internet Explorer and Firefox differ in JavaScript syntax. 

1. The CSS “float” property 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 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
whose ID is “header”, we would use the following syntax: whose ID is “header”, we would use the following syntax: 
  1. document.getElementById( "header" ).style.backgroundColor= "#ccc" ; 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: 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: The IE Syntax:
  1. document.getElementById( "header" ).style.styleFloat = "left" ; document.getElementById( "header" ).style.styleFloat = "left" ; 
The Firefox Syntax: The Firefox Syntax:
  1. document.getElementById( "header" ).style.cssFloat = "left" ; document.getElementById( "header" ).style.cssFloat = "left" ; 

2. The Computed Style of an Element 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: 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: The IE Syntax:
  1. var myObject = document.getElementById( "header" ); var myObject = document.getElementById( "header" );
  2. var mystyle=myObject.currentStyle.backgroundColor; var mystyle=myObject.currentStyle.backgroundColor; 
The Firefox Syntax: The Firefox Syntax:
  1. var myObject = document.getElementById( "header" ); var myObject = document.getElementById( "header" );
  2. var myComputedstyle=document.defaultView.getComputedStyle(myObject, null ); var myComputedstyle=document.defaultView.getComputedStyle(myObject, null );
  3. var mystyle=myComputedStyle.backgroundColor; var mystyle=myComputedStyle.backgroundColor; 

3. Accessing the “class” Attribute of an Element 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 in JavaScript. As is the case with the “float” property, our two major browsers use different syntax to access this attribute in JavaScript. 
The IE Syntax: The IE Syntax:
  1. var myObject = document.getElementById( "header" ); var myObject = document.getElementById( "header" );
  2. var myAttribute = myObject.getAttribute( "className" ); var myAttribute = myObject.getAttribute( "className" ); 
The Firefox Syntax: The Firefox Syntax:
  1. var myObject = document.getElementById( "header" ); var myObject = document.getElementById( "header" );
  2. var myAttribute = myObject.getAttribute( "class" ); var myAttribute = myObject.getAttribute( "class" ); 
This syntax would also apply using the setAttribute method. This syntax would also apply using the setAttribute method. 

4. Accessing the “for” Attribute of the  4. Accessing the “for” Attribute of the 

Similar to number 3, we have different syntax to access a  Similar to number 3, we have different syntax to access a  
The IE Syntax: The IE Syntax:
  1. var myObject = document.getElementById( "myLabel" ); var myObject = document.getElementById( "myLabel" );
  2. var myAttribute = myObject.getAttribute( "htmlFor" ); var myAttribute = myObject.getAttribute( "htmlFor" ); 
The Firefox Syntax: The Firefox Syntax:
  1. var myObject = document.getElementById( "myLabel" ); var myObject = document.getElementById( "myLabel" );
  2. var myAttribute = myObject.getAttribute( "for" ); var myAttribute = myObject.getAttribute( "for" ); 

5. Getting the Cursor Position 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! 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: The IE Syntax:
  1. var myCursorPosition = [0, 0]; var myCursorPosition = [0, 0];
  2. myCursorPosition[0] = event.clientX; myCursorPosition[0] = event.clientX;
  3. myCursorPosition[1] = event.clientY; myCursorPosition[1] = event.clientY; 
The Firefox Syntax: The Firefox Syntax:
  1. var myCursorPosition = [0, 0]; var myCursorPosition = [0, 0];
  2. myCursorPosition[0] = event.pageX; myCursorPosition[0] = event.pageX;
  3. myCursorPosition[1] = event.pageY; myCursorPosition[1] = event.pageY; 

6. Getting the Size of the Viewport, or Browser Window 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”. Sometimes it's necessary to find out the size of the browser's available window space, usually called the “viewport”.

The IE Syntax: The IE Syntax:
  1. var myBrowserSize = [0, 0]; var myBrowserSize = [0, 0];
  2. myBrowserSize[0] = document.documentElement.clientWidth; myBrowserSize[0] = document.documentElement.clientWidth;
  3. myBrowserSize[1] = document.documentElement.clientHeight; myBrowserSize[1] = document.documentElement.clientHeight; 
The Firefox Syntax: The Firefox Syntax:
  1. var myBrowserSize = [0, 0]; var myBrowserSize = [0, 0];
  2. myBrowserSize[0] = window.innerWidth; myBrowserSize[0] = window.innerWidth;
  3. myBrowserSize[1] = window.innerHeight; myBrowserSize[1] = window.innerHeight; 

7. Alpha Transparency 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: 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: The IE Syntax:
  1. #myElem ent { #myElem ent {
  2. filter: alpha(opacity=50); filter: alpha(opacity=50);
  3. } } 
The Firefox Syntax: The Firefox Syntax:
  1. #myElem ent { #myElem ent {
  2. opacity: 0.5; opacity: 0.5;
  3. } } 
So, to access those values via JavaScript, we would use the style object: So, to access those values via JavaScript, we would use the style object: 
The IE Syntax: The IE Syntax:
  1. var myObject = document.getElementById( "myElement" ); var myObject = document.getElementById( "myElement" );
  2. myObject.style.filter = "alpha(opacity=80)" ; myObject.style.filter = "alpha(opacity=80)" ; 
The Firefox Syntax: The Firefox Syntax:
  1. var myObject = document.getElementById( "myElement" ); var myObject = document.getElementById( "myElement" );
  2. myObject.style.opacity = "0.5" ; 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. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值