Javascript Firefox is compatible with DOM method scripts of IE

Source: Internet
Author: User
Tags tagname

If (! Document. All ){
// Zzcv FF ie compatible script
/* Unresolved issues and handling of the script:

2. in IE, you can use () or [] to obtain the collection class object. In Firefox, you can only use [] to obtain the collection class object.
Solution: use [] to retrieve collection class objects.
3. in IE, you can use the method to obtain general attributes to obtain custom attributes, or you can use getattribute () to obtain Custom Attributes. In Firefox, you can only use getattribute () to obtain custom attributes.
Solution: getattribute () is used to obtain custom attributes.
4. in IE, the ID of the HTML object can be directly used as the variable name of the subordinate object of the document; in Firefox, the ID cannot.
5. In Firefox, variable names with the same HTML Object ID can be used; in IE, variable names cannot be used.
Solution: Use document. getelementbyid ("idname") replaces document. idname. we recommend that you do not use variable names with the same HTML Object ID to reduce errors. When declaring variables, add VaR to avoid ambiguity.
6. The input. Type attribute in IE is read-only, but the input. Type attribute in Firefox is read/write.
8. In IE, you can use showmodaldialog and showmodelessdialog to open modal and non-modal windows. In Firefox, you cannot
9. The body of Firefox exists before the body tag is fully read by the browser. The body of IE must exist only after the body tag is fully read by the browser.
10.
*/
// Document compatibility
Htmldocument. Prototype. _ definegetter _ ("all", function (){
Return this. getelementsbyname ("*");});

Htmlformelement. constructor. Prototype. Item = function (s ){
Return this. elements [s];};

Htmlcollection. Prototype. Item = function (s ){
Return this [s] ;};

// Event compatibility
Window. constructor. Prototype. _ definegetter _ ("Event", function (){
For (VAR o = arguments. callee. Caller, E = NULL; o! = NULL; O = O. Caller ){
E = O. Arguments [0];
If (E & (E instanceof event ))
Return e ;}
Return NULL ;});

Window. constructor. Prototype. attachevent = htmldocument. Prototype. attachevent = htmlelement. Prototype. attachevent = function (e, f ){
This. addeventlistener (E. Replace (/^ on/I, ""), F, false );};

Window. constructor. Prototype. detachevent = htmldocument. Prototype. detachevent = htmlelement. Prototype. detachevent = function (e, f ){
This. removeeventlistener (E. Replace (/^ on/I, ""), F, false );};

With (window. event. constructor. Prototype ){
_ Definegetter _ ("srcelement", function (){
Return this.tar get ;});

_ Definesetter _ ("returnvalue", function (B ){
If (! B) This. preventdefault ();});

_ Definesetter _ ("cancelbubble", function (B ){
If (B) This. stoppropagation ();});

_ Definegetter _ ("fromelement", function (){
VaR o = (this. type = "Mouseover" & this. relatedtarget) | (this. type = "mouseout" &&this.tar get) | NULL;
If (o)
While (O. nodetype! = 1)
O = O. parentnode;
Return O ;});

_ Definegetter _ ("toelement", function (){
VaR o = (this. type = "Mouseover" &this.tar get) | (this. type = "mouseout" & this. relatedtarget) | NULL;
If (o)
While (O. nodetype! = 1)
O = O. parentnode;
Return O ;});

_ Definegetter _ ("X", function (){
Return this. pagex ;});

_ Definegetter _ ("Y", function (){
Return this. Pagey ;});

_ Definegetter _ ("offsetx", function (){
Return this. layerx ;});

_ Definegetter _ ("offsety", function (){
Return this. layery ;});
}
// Node operation compatibility
With (window. node. Prototype ){
Replacenode = function (o ){
This. parentnode. replaceChild (O, this );}

Removenode = function (B ){
If (B)
Return this. parentnode. removechild (this );
VaR range = Document. createRange ();
Range. selectnodecontents (this );
Return this. parentnode. replaceChild (range. extractcontents (), this );}

Swapnode = function (o ){
Return this. parentnode. replaceChild (O. parentnode. replaceChild (this, O), this );}

Contains = function (o ){
Return o? (O = This )? True: arguments. callee (O. parentnode): false ;}
}
// Html element compatibility
With (window. htmlelement. Prototype ){
_ Definegetter _ ("parentelement", function (){
Return (this. parentnode = This. ownerdocument )? Null: This. parentnode ;});

_ Definegetter _ ("children", function (){
VaR c = [];
For (VAR I = 0, cs = This. childnodes; I <CS. length; I ++ ){
If (CS [I]. nodetype = 1)
C. Push (CS [I]);}
Return C ;});

_ Definegetter _ ("canhavechildren", function (){
Return! /^ (Area | base | basefont | Col | frame | HR | IMG | BR | input | isindex | link | meta | PARAM) $/I. test (this. tagname );});

_ Definesetter _ ("outerhtml", function (s ){
VaR r = This. ownerdocument. createRange ();
R. setstartbefore (this );
Void this. parentnode. replaceChild (R. createcontextualfragment (s), this );
Return s ;});
_ Definegetter _ ("outerhtml", function (){
VaR as = This. attributes;
VaR STR = "<" + this. tagname;
For (VAR I = 0, Al = As. length; I <al; I ++ ){
If (as [I]. specified)
STR + = "" + as [I]. Name + "=" "+ as [I]. Value + """;}
Return this. canhavechildren? STR + ">": Str + ">" + this. innerhtml + "</" + this. tagname + "> ";});

_ Definesetter _ ("innertext", function (s ){
Return this. innerhtml = Document. createtextnode (s );});
_ Definegetter _ ("innertext", function (){
VaR r = This. ownerdocument. createRange ();
R. selectnodecontents (this );
Return R. tostring ();});

_ Definesetter _ ("outertext", function (s ){
Void this. parentnode. replaceChild (document. createtextnode (s), this );
Return s });
_ Definegetter _ ("outertext", function (){
VaR r = This. ownerdocument. createRange ();
R. selectnodecontents (this );
Return R. tostring ();});

Insertadjacentelement = function (s, O ){
Return (S = "beforebegin" & this. parentnode. insertbefore (O, this) | (S = "afterbegin" & this. insertbefore (O, this. firstchild) | (S = "beforeend" & this. appendchild (o) | (S = "afterend" & (this. nextsibling) & this. parentnode. insertbefore (O, this. nextsibling) | this. parentnode. appendchild (O) | NULL ;}

Insertadjacenthtml = function (s, h ){
VaR r = This. ownerdocument. createRange ();
R. setstartbefore (this );
This. insertadjacentelement (S, R. createcontextualfragment (h ));}

Insertadjacenttext = function (S, T ){
This. insertadjacentelement (S, document. createtextnode (t ));}
}
// Xmldom compatibility
Window. activexobject = function (s ){
Switch (s ){
Case "xmldom ":
Document. Implementation. createdocument. Call (this, "text/XML", "", null );
// Domdoc = Document. Implementation. createdocument ("text/XML", "", null );
Break;
}
}

Xmldocument. Prototype. loadxml = function (s ){
For (VAR I = 0, cs = This. childnodes, CL = childnodes. length; I <CL; I ++)
This. removechild (CS [I]);
This. appendchild (this. importnode (New domparser (). parsefromstring (S, "text/XML" ..doc umentelement, true ));}

xmldocument. prototype. selectsinglenode = element. prototype. selectsinglenode = function (s) {
return this. selectnodes (s) [0] ;}< br> xmldocument. prototype. selectnodes = element. prototype. selectnodes = function (s) {
var RT = [];
for (VAR I = 0, RS = This. evaluate (S, this, this. creatensresolver (this. ownerdocument = NULL? This.doc umentelement: this.ownerdocument.doc umentelement), xpathresult. ordered_node_snapshot_type, null), SL = Rs. snapshotlength; I RT. push (RS. snapshotitem (I);
return RT ;}

Xmldocument. Prototype. _ PROTO _. _ definegetter _ ("XML", function (){
Try {
Return new xmlserializer (). serializetostring (this );}
Catch (e ){
Return document. createelement ("Div"). appendchild (this. clonenode (true). innerhtml ;}});
Element. Prototype. _ PROTO _. _ definegetter _ ("XML", function (){
Try {
Return new xmlserializer (). serializetostring (this );}
Catch (e ){
Return document. createelement ("Div"). appendchild (this. clonenode (true). innerhtml ;}});

Xmldocument. Prototype. _ PROTO _. _ definegetter _ ("text", function (){
Return this. firstchild. textcontent ;});

Element. Prototype. _ PROTO _. _ definegetter _ ("text", function (){
Return this. textcontent ;});
Element. Prototype. _ PROTO _. _ definesetter _ ("text", function (s ){
Return this. textcontent = s ;});

}

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.