Summary of Javascript compatibility between IE and Firefox [Recommended collection]_javascript skills
WBOY
Release: 2016-05-16 18:00:47
Original
891 people have browsed it
JavaScript compatibility has long been a major issue for web developers. Many developers struggle with the differences between formal specifications, de facto standards, and implementations. To this end, we mainly summarize the Javascript compatibility of IE and Firefox from the following aspects:
1. Differences in functions and methods; 2. Style access and settings; 3. DOM methods and objects Quote; 4. Event processing; 5. Compatible processing of other differences.
1. Differences between functions and methods 1. getYear() method [Analysis explanation] First look at the following code:
The date you get in IE is "2010", and the date you see in Firefox is "110", mainly because getYear in Firefox returns the value of "current year-1900" .
[Compatible processing] Add the judgment of the year, such as:
[Analysis description] In IE, You can use eval("idName") or getElementById("idName") to get the HTML object with the id idName; under Firefox, you can only use getElementById("idName") to get the HTML object with the id idName. [Compatibility processing] Use getElementById("idName") uniformly to obtain the HTML object with the id idName.
3. const statement [Analysis explanation] The const keyword cannot be used in IE. For example: const constVar = 32; This is a syntax error in IE. [Compatibility processing] Do not use const and use var instead.
4. var [Analysis instructions] Please see the following code:
This function runs normally on IE, but not on Firefox An error was reported.
[Compatibility processing] It is normal to add var before echo. This is the purpose of us mentioning var.
5. const problem [Analysis explanation] The const keyword cannot be used in IE. Such as const constVar = 32; This is a syntax error in IE. 【Solution】Do not use const and use var instead.
2. Style access and setting 1. CSS "float" attribute [Analysis explanation] The most basic syntax for Javascript to access a given CSS value is: object .style.property, but some CSS properties have the same names as reserved words in Javascript, such as "float", "for", "class", etc. Different browsers have different writing methods. Write like this in IE: document.getElementById("header").style.styleFloat = "left"; Write like this in Firefox: document.getElementById("header"). style.cssFloat = "left"; [Compatibility processing] Add a judgment before writing to determine whether the browser is IE: if(document.all){ document.getElementById("header") .style.styleFloat = "left"; } else{ document.getElementById("header").style.cssFloat = "left"; }