Let FF and IE stay closer

Source: Internet
Author: User
Tags range return tagname tostring window

Added IE proprietary properties and methods to FF:

<script language= "JavaScript" type= "Text/javascript" >
<!--
if (window. Event) {//fix DOM for event
event.prototype.__definesetter__ ("ReturnValue", function (b) {//
if (!b) This.preventdefault ();
return b;
});
event.prototype.__definesetter__ ("cancelbubble", function (b) {//Set or retrieve the level bubbling of the current event handle
if (b) this.stoppropagation ();
return b;
});
event.prototype.__definegetter__ ("Srcelement", function () {
var node=this.target;
while (node.nodetype!=1) Node=node.parentnode;
return node;
});
event.prototype.__definegetter__ ("Fromelement", function () {//Returns the source node of the mouse movement
var node;
if (this.type== "MouseOver")
Node=this.relatedtarget;
else if (this.type== "Mouseout")
Node=this.target;
if (!node) return;
while (node.nodetype!=1) Node=node.parentnode;
return node;
});
event.prototype.__definegetter__ ("Toelement", function () {//returns the mouse-moved source node
var node;
if (this.type== "Mouseout")
Node=this.relatedtarget;
else if (this.type== "MouseOver")
Node=this.target;
if (!node) return;
while (node.nodetype!=1) Node=node.parentnode;
return node;
});
event.prototype.__definegetter__ ("OffsetX", function () {
return This.layerx;
});
event.prototype.__definegetter__ ("OffsetY", function () {
return this.layery;
});
}
if (window. Document) {//correcting DOM for document
}
if (window. node) {//Correcting node's DOM
Node.prototype.replacenode=function (node) {///replace specified nodes
This.parentNode.replaceChild (Node,this);
}
Node.prototype.removenode=function (Removechildren) {//delete specified node
if (Removechildren)
Return This.parentNode.removeChild (this);
else{
var range=document.createrange ();
Range.selectnodecontents (this);
Return This.parentNode.replaceChild (Range.extractcontents (), this);
}
}
Node.prototype.swapnode=function (node) {//Exchange node
var nextsibling=this.nextsibling;
var Parentnode=this.parentnode;
Node.parentNode.replaceChild (This,node);
Parentnode.insertbefore (node,nextsibling);
}
}
if (window. HtmlElement) {
HTMLELEMENT.PROTOTYPE.__DEFINEGETTER__ ("All", function () {
var a=this.getelementsbytagname ("*");
var node=this;
A.tags=function (stagname) {
Return Node.getelementsbytagname (Stagname);
}
return A;
});
htmlelement.prototype.__definegetter__ ("Parentelement", function () {
if (this.parentnode==this.ownerdocument) return null;
return this.parentnode;
});
htmlelement.prototype.__definegetter__ ("Children", function () {
var tmp=[];
var j=0;
var n;
for (Var i=0;i<this.childnodes.length;i++) {
N=this.childnodes[i];
if (n.nodetype==1) {
Tmp[j++]=n;
if (n.name) {
if (!tmp[n.name])
Tmp[n.name]=[];
Tmp[n.name][tmp[n.name].length]=n;
}
if (n.id)
Tmp[n.id]=n;
}
}
return TMP;
});
htmlelement.prototype.__definegetter__ ("Currentstyle", function () {
Return This.ownerDocument.defaultView.getComputedStyle (This,null);
});
htmlelement.prototype.__definesetter__ ("outerHTML", function (SHTML) {
var r=this.ownerdocument.createrange ();
R.setstartbefore (this);
var df=r.createcontextualfragment (SHTML);
This.parentNode.replaceChild (Df,this);
return SHTML;
});
htmlelement.prototype.__definegetter__ ("outerHTML", function () {
var attr;
var attrs=this.attributes;
var str= "<" +THIS.TAGNAME;
for (Var i=0;i<attrs.length;i++) {
Attr=attrs[i];
if (attr.specified)
str+= "" +attr.name+ "=" "+attr.value+" "";
}
if (!this.canhavechildren)
return str+ ">";
return str+ ">" +this.innerhtml+ "</" +this.tagname+ ">";
});
htmlelement.prototype.__definegetter__ ("Canhavechildren", function () {
Switch (This.tagName.toLowerCase ()) {
Case "Area":
Case "Base":
Case "Basefont":
Case "Col":
Case "Frame":
Case "HR":
Case "IMG":
Case "BR":
Case "Input":
Case "Isindex":
Case "link":
Case "META":
Case "param":
return false;
}
return true;
});
htmlelement.prototype.__definesetter__ ("InnerText", function (stext) {
var parsedtext=document.createtextnode (stext);
This.innerhtml=parsedtext;
return parsedtext;
});
htmlelement.prototype.__definegetter__ ("InnerText", function () {
var r=this.ownerdocument.createrange ();
R.selectnodecontents (this);
return r.tostring ();
});
htmlelement.prototype.__definesetter__ ("Outertext", function (stext) {
var parsedtext=document.createtextnode (stext);
This.outerhtml=parsedtext;
return parsedtext;
});
htmlelement.prototype.__definegetter__ ("Outertext", function () {
var r=this.ownerdocument.createrange ();
R.selectnodecontents (this);
return r.tostring ();
});
Htmlelement.prototype.attachevent=function (Stype,fhandler) {
var shorttypename=stype.replace (/on/, "");
Fhandler._ieemueventhandler=function (e) {
Window.event=e;
return Fhandler ();
}
This.addeventlistener (Shorttypename,fhandler._ieemueventhandler,false);
}
Htmlelement.prototype.detachevent=function (Stype,fhandler) {
var shorttypename=stype.replace (/on/, "");
if (typeof (Fhandler._ieemueventhandler) = = "function")
This.removeeventlistener (Shorttypename,fhandler._ieemueventhandler,false);
Else
This.removeeventlistener (shorttypename,fhandler,true);
}
Htmlelement.prototype.contains=function (node) {//Does it contain a
Do if (node==this) return true;
while (Node=node.parentnode);
return false;
}
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
}
}
Htmlelement.prototype.insertadjacenthtml=function (WHERE,HTMLSTR) {
var r=this.ownerdocument.createrange ();
R.setstartbefore (this);
var parsedhtml=r.createcontextualfragment (HTMLSTR);
This.insertadjacentelement (where,parsedhtml);
}
Htmlelement.prototype.insertadjacenttext=function (WHERE,TXTSTR) {
var parsedtext=document.createtextnode (TXTSTR);
This.insertadjacentelement (Where,parsedtext);
}
Htmlelement.prototype.attachevent=function (Stype,fhandler) {
var shorttypename=stype.replace (/on/, "");
Fhandler._ieemueventhandler=function (e) {
Window.event=e;
return Fhandler ();
}
This.addeventlistener (Shorttypename,fhandler._ieemueventhandler,false);
}
Htmlelement.prototype.detachevent=function (Stype,fhandler) {
var shorttypename=stype.replace (/on/, "");
if (typeof (Fhandler._ieemueventhandler) = = "function")
This.removeeventlistener (Shorttypename,fhandler._ieemueventhandler,false);
Else
This.removeeventlistener (shorttypename,fhandler,true);
}
}
-->
</script>

For example, use Currentstyle in FF

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<style type= "Text/css" >
#a {width:100px;height:100px;background-color:yellow;}
</style>
<script type= "Text/javascript" >
Window.onload=function () {
htmlelement.prototype.__definegetter__ ("Currentstyle", function () {return This.ownerDocument.defaultView.getComputedStyle (this,null);});
The preceding line of code adds the Currentstyle attribute to all HTML elements
var A=document.getelementbyid ("a");
Alert ("Width=\ in Style", "+a.style.width+" \ \tcurrentstyle width=\ "" +a.currentstyle.width+ "\");
}
</script>
<div id= "a" ></div>
</body>



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.