Home  >  Article  >  Web Front-end  >  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
WBOYOriginal
2016-05-16 19:07:44967browse

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; 
    } 

}
Statement:
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