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 ();