Unified interface: Add JavaScript code for IE methods and attributes to FireFox

Source: Internet
Author: User

How to run code (JS only) in Z-Blog cannot display the "run code" link because FF does not support insertAdjacentElement. Today, I Googled and found a good article. I saved the following script into an iedom4moz. js file, and called -- OK on every page. Everything is done! Happy, just as happy as everyone, I shared it with you ^_^ Copy codeThe Code is as follows: // JavaScript Document
// Unified interface: Add IE methods and attributes for FireFox
If (window. Event) {// corrected the DOM of the Event
/*
IE5 MacIE5 Mozilla Konqueror2.2 Opera5
Event yes
Event. returnValue yes no
Event. cancelBubble yes no
Event. srcElement yes no
Event. fromElement yes no
*/
Event. prototype. _ defineSetter _ ("returnValue", function (B ){//
If (! B) this. preventDefault ();
Return B;
});
Event. prototype. _ defineSetter _ ("cancelBubble", function (B) {// sets or retrieves the layer bubbles of the current Event handle
If (B) this. stopPropagation ();
Return B;
});
Event. prototype. _ defineGetter _ ("srcElement", function (){
Var node‑this.tar get;
While (node. nodeType! = 1) node = node. parentNode;
Return node;
});
Event. prototype. _ defineGetter _ ("fromElement", function () {// return the source node removed from the mouse
Var node;
If (this. type = "mouseover ")
Node = this. relatedTarget;
Else if (this. type = "mouseout ")
Node‑this.tar get;
If (! Node) return;
While (node. nodeType! = 1) node = node. parentNode;
Return node;
});
Event. prototype. _ defineGetter _ ("toElement", function () {// return the source node to which the cursor is moved
Var node;
If (this. type = "mouseout ")
Node = this. relatedTarget;
Else if (this. type = "mouseover ")
Node‑this.tar get;
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) {// modify the DOM of the Document
/*
IE5 MacIE5 Mozilla Konqueror2.2 Opera5
Document.doc umentElement yes no
Document. activeElement yes null no
*/
}
If (window. Node) {// corrected Node DOM
/*
IE5 MacIE5 Mozilla Konqueror2.2 Opera5
Node. contains yes no yes
Node. replaceNode yes no
Node. removeNode yes no
Node. children yes no
Node. hasChildNodes yes no
Node. childNodes yes no
Node. swapNode yes no
Node. currentStyle yes no
*/
Node. prototype. replaceNode = function (Node) {// Replace the specified Node
This. parentNode. replaceChild (Node, this );
}
Node. prototype. removeNode = function (removeChildren) {// delete a 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;
});
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 polictypename = sType. replace (/on /,"");
FHandler. _ ieEmuEventHandler = function (e ){
Window. event = e;
Return fHandler ();
}
This. addEventListener (optional typename, fHandler. _ ieEmuEventHandler, false );
}
HTMLElement. prototype. detachEvent = function (sType, fHandler ){
Var polictypename = sType. replace (/on /,"");
If (typeof (fHandler. _ ieEmuEventHandler) = "function ")
This. removeEventListener (optional typename, fHandler. _ ieEmuEventHandler, false );
Else
This. removeEventListener (polictypename, fHandler, true );
}
HTMLElement. prototype. contains = function (Node) {// whether a Node is included
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 polictypename = sType. replace (/on /,"");
FHandler. _ ieEmuEventHandler = function (e ){
Window. event = e;
Return fHandler ();
}
This. addEventListener (optional typename, fHandler. _ ieEmuEventHandler, false );
}
HTMLElement. prototype. detachEvent = function (sType, fHandler ){
Var polictypename = sType. replace (/on /,"");
If (typeof (fHandler. _ ieEmuEventHandler) = "function ")
This. removeEventListener (optional typename, fHandler. _ ieEmuEventHandler, false );
Else
This. removeEventListener (polictypename, fHandler, true );
}
}
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.