I recently sorted out the browser compatibility issues and made a big difference. With the help of my predecessors, I still made some progress. I 'd like to post some code below, which I think will be more useful.
Copy codeThe Code is as follows:
Var isIE = ????;
// Global variable to determine whether it is ie and 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;
// Define node xml attributes
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 );
} Catch (ex ){
Var d = document. createElement ("div ");
D. appendChild (this. cloneNode (true ));
Return d. innerHTML;
}
});
// Define the node text attribute
XMLDocument. prototype. _ proto _. _ defineGetter _ ("text", function (){
Return this. firstChild. textContent
});
Element. prototype. _ proto _. _ defineGetter _ ("text", function (){
Return this. textContent
});
// Define the selectSingleNode and selectNodes Methods
XMLDocument. prototype. selectSingleNode = Element. prototype. selectSingleNode = 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.doc umentElement );
Var result = xpe. evaluate (xpath, this, nsResolver, 0, null );
Var found = [];
Var res;
While (res = result. iterateNext ())
Found. push (res );
Return found;
}
}