Home > Web Front-end > JS Tutorial > Compatibility issues related to dom and xml node access under javascript_javascript skills

Compatibility issues related to dom and xml node access under javascript_javascript skills

WBOY
Release: 2016-05-16 19:07:44
Original
999 people have browsed it

Recently I have been sorting out browser compatibility issues, which has been a big deal. With the help of predecessors, I have made some progress. I will post some code below, I think it will be more useful

Copy code The code is as follows:

var isIE = ????;
//Global variable, determine whether IE, self-improvement
// new dom method
function parseXML(st){
if ( isIE){
var result = new ActiveXObject( "microsoft.XMLDOM" );
result.loadXML(st);
} else {
var parser = new DOMParser();
var result = parser.parseFromString(st, "text/xml" );
}
return result;
}
if (! isIE){
var ex;
// Definition Node xml attribute
XMLDocument.prototype.__proto__.__defineGetter__( "xml" , function (){
try {
return new XMLSerializer().serializeToString( this );
          } catch (ex){
var d = document.createElement("div");
d.appendChild(this .cloneNode(true));
return d.innerHTML;                                                                                                                                     > Element.prototype.__proto__.__defineGetter__( "xml" , function (){
try {
return new XMLSerializer().serializeToString(this);
} c atch (ex){
var d = document.createElement( "div" );
d.appendChild( this .cloneNode( true ));
return d.innerHTML;
}
//Define nodes text attribute
"text" , function (){
return this .textContent
});
// Define selectSingleNode, selectNodes methods
XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingle Node = function (xpath){
var x = this .selectNodes(xpath)
if ( ! x || x.length < 1 ) return null ;
return x[0];

    XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (xpath){ 
         var  xpe  =   new  XPathEvaluator(); 
         var  nsResolver  =  xpe.createNSResolver( this .ownerDocument  ==   null   ?  
             this .documentElement :  this .ownerDocument.documentElement); 
         var  result  =  xpe.evaluate(xpath,  this , nsResolver,  0 ,  null ); 
         var  found  =  []; 
         var  res; 
         while  (res  =  result.iterateNext()) 
            found.push(res); 
         return  found; 
    } 

}
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template