JavaScript Firefox compatible with IE Dom method scripting _javascript Tips

Source: Internet
Author: User
Tags tagname
if (!document.all) {
ZZCV FF IE compatible script
/* Script does not solve the problem and processing:

2.IE, you can use () or [] to get the collection class object; Firefox, you can only use [] to get collection class objects.
WORKAROUND: Use [] to get collection class objects uniformly.
3.IE, you can obtain a custom attribute by using a method that gets the general properties, or you can use GetAttribute () to get the definition attribute; Firefox, you can only use getattribute () to get the definition attribute.
Workaround: The unification is obtained from the defined attribute through GetAttribute ().
4.IE, the ID of an HTML object can be used directly as the subordinate object variable name of document; Firefox is not.
5.Firefox, you can use the same variable name as the HTML object ID;
Workaround: Use document.getElementById ("Idname") instead of document.idname. It is best not to take a variable name with the same HTML object ID to reduce the error; When declaring a variable, add var to avoid ambiguity.
The Input.type property under 6.IE is read-only, but the Input.type property under Firefox is read-write.
8.IE, the modal and modeless windows can be opened by ShowModalDialog and showModelessDialog. Firefox is not
The body label of the 9.Firefox is present before the body tag is fully read into the browser, and the IE body must be fully read by the browser before the body tag is present.
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.target;});

__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.target) | | Null
if (o)
while (o.nodetype!=1)
O=o.parentnode;
return o;});

__definegetter__ ("Toelement", function () {
var o= (this.type== "mouseover" &&this.target) | | (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 compatible
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 compatible
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 compatible
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"). Documentelement,true);

Xmldocument.prototype.selectsinglenode=element.prototype.selectsinglenode=function (s) {
return This.selectnodes (s) [0];}
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.documentelement: this.ownerDocument.documentElement), Xpathresult.ordered_node_snapshot_type,null),sl=rs.snapshotlength;i<sl; i++)
Rt.push (Rs.snapshotitem (i));
return RT;}

XMLDOCUMENT.PROTOTYPE.__PROTO__.__DEFINEGETTER__ ("XML", function () {
try{
Return to New XMLSerializer (). serializetostring (this);}
catch (e) {
return document.createelement ("div"). AppendChild (This.clonenode (true). InnerHTML;});
ELEMENT.PROTOTYPE.__PROTO__.__DEFINEGETTER__ ("XML", function () {
try{
Return to 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.