Home Web Front-end JS Tutorial Unified interface: js code for adding IE methods and attributes to FireFox_javascript skills

Unified interface: js code for adding IE methods and attributes to FireFox_javascript skills

May 16, 2016 pm 07:15 PM

In the article How to Run Code in Z-Blog (Pure JS Version), because FF does not support insertAdjacentElement, the "Run Code" link cannot be displayed. I Googled it today and found a good article. Save the following script as iedom4moz.js file and call it on every page - OK, everything is done! It is better to have fun alone than to have fun with everyone, so I share it with you ^_^

Copy the code The code is as follows:

// JavaScript Document
// Unified interface: Add IE methods and attributes for FireFox
if(window.Event){// Modify the DOM of Event
/*
IE5 MacIE5 Mozilla Konqueror2.2 Opera5
event yes yes yes
event.returnValue yes no no no 
event.cancelBubble yes no no no 
event.srcElement yes yes no no
event.fromElement yes no no
*/
Event.prototype.__defineSetter__("returnValue",function(b){//
if(!b)this.preventDefault ();
return b;
});
Event.prototype.__defineSetter__("cancelBubble",function(b){// Set or retrieve the hierarchical bubble of the current event handler
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(){// Return the source node that the mouse moved out of
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(){// Return the source node that the mouse moved into
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){// 修正Document的DOM 
  /* 
                IE5    MacIE5    Mozilla    Konqueror2.2    Opera5 
  document.documentElement  yes    yes      yes      yes          no 
  document.activeElement    yes    null    no      no          no 
  */ 
  } 
if(window.Node){// 修正Node的DOM 
  /* 
                IE5    MacIE5    Mozilla    Konqueror2.2    Opera5 
  Node.contains        yes    yes      no      no          yes 
  Node.replaceNode      yes    no      no      no          no 
  Node.removeNode        yes    no      no      no          no 
  Node.children        yes    yes      no      no          no 
  Node.hasChildNodes      yes    yes      yes      yes          no 
  Node.childNodes        yes    yes      yes      yes          no 
  Node.swapNode        yes    no      no      no          no 
  Node.currentStyle      yes    yes      no      no          no 
  */ 
  Node.prototype.replaceNode=function(Node){// 替换指定节点 
    this.parentNode.replaceChild(Node,this); 
    } 
  Node.prototype.removeNode=function(removeChildren){// 删除指定节点 
    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){// 交换节点 
    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      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      attr=attrs[i]; 
      if(attr.specified) 
        str =" " attr.name '="' attr.value '"'; 
      } 
    if(!this.canHaveChildren) 
      return str ">"; 
    return str ">" this.innerHTML ""; 
    }); 
  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){// 是否包含某节点 
    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); 
    } 
  } 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles