How to solve the JavaScript compatibility problem between IE and FireFox _ javascript skills

Source: Internet
Author: User
This article provides a detailed analysis of the solutions to the JavaScript compatibility problem between IE and FireFox. If you need a friend, refer to it, I hope to help you in the following situations during development:

1. dynamically delete a row in the table.

Table: indicates a table object.

K: indicates the row number.

Table. rows [k]. removeNode (true); // firefox execution failed, ie execution successful

Compatibility between IE and FireFox

Table. deleteRow (k );

2. Custom Attributes for HTML tags.

InputElement: indicates a form element.

PropertyName: indicates an attribute under a form element.

InputElement. propertyName; // firefox execution failed, ie execution successful

Compatibility between IE and FireFox

Document. getElementById ("txtInput"). attributes ["idvalue"]. nodeValue

3. insert HTML elements at the specified position.

InputElement: indicates a form element.

VDiv: indicates the HTML element to be inserted.

InputElement. insertAdjacentElement ("AfterEnd", vDiv); // firefox execution failed, ie execution successful

Compatibility between IE and FireFox

The method is not defined in firefox. Therefore, if you need to call this method, you need to re-define it.

The Code is as follows:


// Override the insertAdjacentElement () method because it is not found in firefox
HTMLElement. prototype. insertAdjacentElement = function (where, parsedNode ){
Switch (where ){
Case "beforeBegin ":
This. parentNode. insertBefore (parsedNode, this );
Break;
Case "afterBegin ":
This. insertBefore (parsedNode, this. firstChild );
Break;
Case "beforeEnd ":
This. appendChild (parsedNode );
Break;
Case "afterEnd ":
If (this. nextSibling)
This. parentNode. insertBefore (parsedNode, this. nextSibling );
Else
This. parentNode. appendChild (parsedNode );
Break;
}
}


4. The break statement is invalid.

When the for loop statement is executed in IE, break can be used to exit the current loop. But in FF, the entire loop is exited. In this case, use the continue statement.

5. firefox returns String contains an invalid character.

Var chkBox = document. createElement (''); // Executed successfully in IE

Compatibility between IE and FireFox

Firefox does not support the createElement definition method. You need to perform the following steps:

The Code is as follows:


Var chkBox = document. createElement ('input ');
ChkBox. name = "treeBox ";
ChkBox. type = "checkbox ";

ChkBox. value = key;


6. A set of table objects (table rows)
BdList. rows (k). cells (0). innerHTML = "aaa"; // firefox fails and ie is successfully executed.

Compatibility between IE and FireFox

The Code is as follows:


BdList. rows [k]. cells [0]. innerHTML = "aaa ";


7. JS getYear () method in firefox

Var today = new date ();
Var year = today. getYear ();
In Firefox, the value returned by getYear is "current year-1900" in IE, then:
When the today year is less than 2000, it is the same as firefox. So it is best to use getFullYear getUTCFullYear to call

Compatibility between IE and FireFox

The Code is as follows:


Var today = new date ();
Var year = today. getFullYear ();

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.