JavaScript compatibility problem solving method for IE and Firefox _javascript tips

Source: Internet
Author: User
Tags html tags

Here are some of the things I've encountered in development:

1, delete a row in the table dynamically.

Table: Represents a Table object.

K: Indicates line number

Table.rows[k].removenode (TRUE); Firefox execution failed, ie executed successfully

IE and Firefox compatibility

Table.deleterow (k);

2, custom attributes for HTML tags.

Inputelement: Represents a FORM element.

PropertyName: Represents a property under a FORM element

Inputelement.propertyname; Firefox execution failed, ie executed successfully

IE and Firefox compatibility

document.getElementById ("Txtinput"). attributes["Idvalue"].nodevalue

3. Inserts an HTML element at the specified location.

Inputelement: Represents a FORM element.

Vdiv: Represents the HTML element that will be inserted.

Inputelement.insertadjacentelement ("Afterend", Vdiv)//firefox execution failed, ie executed successfully

IE and Firefox compatibility

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

Copy Code code as follows:

Overriding the Insertadjacentelement () method because there is no such method 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, break statement failure.

When executing a For loop statement in IE, the break can be used to jump out of the secondary loop. But in FF it becomes the exit of the entire loop. Instead, use the Continue statement.

5, Firefox newspaper string contains an invalid character.

var chkbox=document.createelement (' <input type= checkbox ' name= ' Treebox ' value= ' +key+ ' > '); Successful execution under IE

IE and Firefox compatibility

Firefox does not support this createelement definition, and needs to be done in a step-by-step manner:

Copy Code code as follows:

var chkbox = document.createelement (' input ');
Chkbox.name = "Treebox";
Chkbox.type = "checkbox";

Chkbox.value = key;


6. A collection of (table row) objects for a Table object
Bdlist.rows (k). Cells (0). InnerHTML = "<a>aaa</a>";//firefox execution failed, ie executed successfully

IE and Firefox compatibility

Copy Code code as follows:

bdlist.rows[k].cells[0].innerhtml = "<a>aaa</a>";

7, JS getyear () method in the Firefox problem

var today = new Date ();
var year = Today.getyear ();
Inside Firefox getyear Returns the value of "Current year-1900" inside IE:
When today's year is less than 2000, it's the same as Firefox. So it's best to use getFullYear getutcfullyear to call

IE and Firefox compatibility

Copy Code code 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.