Compatibility with dom and xml node access in javascript _ javascript skills

Source: Internet
Author: User
The compatibility of dom and xml node access in javascript recently solved the problem of browser compatibility. With the help of our predecessors, I made some progress and wrote some code below, I think it will be more useful

The 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 ("p ");
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 ("p ");
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;
}

}

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.