Home >Web Front-end >JS Tutorial >Solutions to JavaScript compatibility issues between IE and FireFox_javascript tips

Solutions to JavaScript compatibility issues between IE and FireFox_javascript tips

WBOY
WBOYOriginal
2016-05-16 17:05:581049browse

The following is what I encountered during development:

1. Dynamically delete a row in the table.

table: represents table object.

k: represents line number

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

IE and FireFox compatible writing

table.deleteRow(k);

2. Customize attributes for HTML tags.

inputElement: represents a form element.

propertyName: represents a certain property under the form element

inputElement.propertyName; //firefox execution failed, ie execution succeeded

IE and FireFox compatible writing method

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

3. Insert an HTML element at the specified position.

inputElement: Represents a form element.

vDiv: Represents the HTML element to be inserted.

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

IE and FireFox compatible writing

In Firefox, there is no definition of this method, so if you need to call this method, you need to redefine it yourself.

Copy code The code is as follows:

//Rewrite the insertAdjacentElement() method, because in firefox There is no such method
HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
switch(where){
case "beforeBegin":
This.parentNode.insertBefore(parsedNode,this);
                                                                                                                    break;
                                                                                                                                                                                               🎜>                                                                                                                   This.parentNode.insertBefore(parsedNode,this.nextSibling);
else
this.parentNode.appendChild( parsedNode);
break;
}
}


4. The break statement is invalid.

When executing a for loop statement in IE, break can be used to jump out of the current loop. But in FF, it becomes exiting the entire loop. At this time, use the continue statement instead.

5. Firefox reports that String contains an invalid character.

var chkBox=document.createElement(''); //Successfully executed under IE

IE and FireFox compatible writing

Firefox does not support this way of defining createElement and needs to be done step by step:

Copy the code The code is as follows:

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

chkBox.value = key;


6. A collection of table objects (table rows) objects
bdList.rows(k ).cells(0).innerHTML = "aaa";//firefox execution failed, ie execution was successful

IE and FireFox compatible writing

Copy code The code is as follows:

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

7. JS getYear() Method problem in firefox

var today = new date();
var year = today.getYear();
In Firefox, getYear returns the value of "current year-1900". In IE:
When today When the year is less than 2000, it is the same as Firefox. So it’s best to use getFullYear getUTCFulYear to call

IE and FireFox compatible writing

Copy code The code is as follows:

var today = new date();
var year = today.getFullYear();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn