7 JavaScript differences between Firefox and IE

Source: Internet
Author: User

Although the history of using lengthy and annoying code blocks in JavaScript to mark specific browsers has ended, however, it is necessary to occasionally use some simple code blocks and object detection to ensure that some code works normally on the user's machine.

BKJIA recommended reading: different performance of JavaScript in IE and FireFox

1. CSS "float" attributes

The basic syntax for getting a specific CSS attribute of a given object is the object. style attribute, and the attributes with hyphens must be replaced by the camel naming method. For example, to obtain the background-color attribute of a div whose ID is "header", we need to use the following syntax:

 
 
  1. document.getElementById("header").style.borderBottom= "1px solid #ccc"; 
  2.  

However, because "float" is a reserved word in JavaScript, we cannot use object. style. float to obtain the "float" attribute. Here is how we use these two browsers:

IE Syntax:

 
 
  1. document.getElementById("header").style.styleFloat = "left";
  2.  

Firefox Syntax:

 
 
  1. document.getElementById("header").style.cssFloat = "left"; 
  2.  

2. Calculation style of Elements

By using the above object. style. property, JavaScript can easily obtain and modify the CSS style of an object. However, the limitation of this syntax is that it can only get the inline style in HTML, or directly use the style set by JavaScript. The style object cannot obtain the styles set using the external style table. To obtain the "calculation style" of an object, use the following code:

IE Syntax:

 
 
  1. var myObject = document.getElementById("header");  
  2. var myStyle = myObject.currentStyle.backgroundColor; 

Firefox Syntax:

 
 
  1. var myObject = document.getElementById("header");  
  2. var myComputedStyle = document.defaultView.getComputedStyle(myObject, null);  
  3. var myStyle = myComputedStyle.backgroundColor; 

3. Obtain the "class" attribute of an element.

Similar to the "float" attribute, the two browsers use different JavaScript methods to obtain this attribute.

IE Syntax:

 
 
  1. var myObject = document.getElementById("header");  
  2. var myAttribute = myObject.getAttribute("className"); 

Firefox Syntax:

 
 
  1. var myObject = document.getElementById("header");  
  2. var myAttribute = myObject.getAttribute("class"); 

4. Obtain the "for" attribute of the label.

As with the third question, using JavaScript to obtain the label's "for" attribute also has different syntaxes.

IE Syntax:

 
 
  1. var myObject = document.getElementById("myLabel");  
  2. var myAttribute = myObject.getAttribute("htmlFor"); 

Firefox Syntax:

 
 
  1. var myObject = document.getElementById("myLabel");  
  2. var myAttribute = myObject.getAttribute("for"); 

The same syntax is used for the setAtrribute method.

5. Obtain the cursor position

It is rare to obtain the cursor position of an element. If you need to do so, the syntax of IE and Firefox is different. This sample code is quite basic and is generally used as part of processing many complex events. Here it is only used to describe the differences. It should be noted that the results in IE are different from those in Firefox, so this method has some problems.

IE Syntax:

 
 
  1. var myCursorPosition = [0, 0];  
  2. myCursorPosition[0] = event.clientX;  
  3. myCursorPosition[1] = event.clientY; 

Firefox Syntax:

 
 
  1. var myCursorPosition = [0, 0];  
  2. myCursorPosition[0] = event.pageX;  
  3. myCursorPosition[1] = event.pageY; 

6. Obtain the size of the window or browser window

Sometimes you need to find out the size of the valid window space of the browser, which is generally "window ".

IE Syntax:

 
 
  1. var myBrowserSize = [0, 0];  
  2. myBrowserSize[0] = document.documentElement.clientWidth;  
  3. myBrowserSize[1] = document.documentElement.clientHeight; 

Firefox Syntax:

 
 
  1. var myBrowserSize = [0, 0];  
  2. myBrowserSize[0] = window.innerWidth;  
  3. myBrowserSize[1] = window.innerHeight; 

7. Alpha transparency

This is actually not a JavaScript syntax project-alpha transparency is set through CSS. However, when the object is set to fade in and out through JavaScript, it needs to be implemented by obtaining the alpha setting of CSS, which is generally within the loop. Use the following JavaScript to change the CSS code:

IE Syntax:

 
 
  1. #myElement {  
  2. filter: alpha(opacity=50);  

Firefox Syntax:

 
 
  1. #myElement {  
  2. opacity: 0.5;  

To obtain these values using JavaScript, you need to use the style object:

IE Syntax:

 
 
  1. var myObject = document.getElementById("myElement");  
  2. myObject.style.filter = "alpha(opacity=80)"; 

Firefox Syntax:

 
 
  1. var myObject = document.getElementById("myElement");  
  2. myObject.style.opacity = "0.5"; 

Of course, we have already mentioned that it is generally used to change opcity/alpha in the middle of a loop to create an animation effect. But this is a simple example, to clearly describe how the method is implemented.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.