JS in Browser compatibility Tutorial: function and Method differences

Source: Internet
Author: User
Tags date object eval

Article Introduction: JavaScript compatibility has long been a major problem for Web developers. The differences between formal norms, factual standards and various implementations make many developers suffer day and night. To this end, mainly from the following aspects of the differences to summarize IE and Firefox javascript compatibility.

1. getyear () method

"Analysis Notes" first look at the following code:

var year= new Date (). getyear ();
document.write (year);

In IE, the date is "2010", in Firefox, see the date is "110", mainly because in Firefox inside GetYear return is "Current year-1900" value.

"Compatibility Handling"

Plus the judgment of the year, such as:

var year= new Date (). getyear ();
Year = (year<1900? ( 1900+year): Year); document.write (year);

It can also be invoked via getFullYear getUTCFullYear:

var year = new Date (). getFullYear ();
document.write (year);

2. Eval () function

"Analysis Notes" in IE, you can use either eval ("Idname") or getElementById ("Idname") to get an HTML object with an ID of Idname; Firefox can only use getElementById ("Idname") To get the HTML object with the ID idname.

"Compatible processing" unifies getElementById ("Idname") to obtain an HTML object with an ID of idname.

3. Const statement

The Const keyword cannot be used in IE by the analysis note. Such as:

Const CONSTVAR = 32;

This is a syntax error in IE.

"Compatibility handling" does not use const and is substituted with var.

4. var

For an explanation of the analysis, see the following code:

Echo=function (str) {
document.write (str);
}

This function in IE on the normal operation, Firefox under the error.

"Compatible processing" and adding Var before echo is normal, and this is what we refer to as the purpose of Var.

5. The question of the const

The Const keyword cannot be used in IE by the analysis note. such as const CONSTVAR = 32; This is a syntax error in IE.

"Workaround" does not use const and is substituted with var.



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.