JavaScript _javascript tips on DOM and XML node access compatibility issues

Source: Internet
Author: User
Tags xpath
Recently, the problem of cleaning up the browser compatibility, the real head, with the help of predecessors, or a little progress, the following post some code, I think will be more useful

Copy Code code as follows:

var isie =????;
Global variables to 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;
Defining node XML attributes
XMLDOCUMENT.PROTOTYPE.__PROTO__.__DEFINEGETTER__ ("XML", function () {
try {
Return to 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 to New XMLSerializer (). serializetostring (this);
catch (ex) {
var d = document.createelement ("div");
D.appendchild (this. CloneNode (true));
return d.innerhtml;
}
});
Define node Text property
xmldocument.prototype.__proto__.__definegetter__ ("Text", function () {
return this. firstchild.textcontent
});
element.prototype.__proto__.__definegetter__ ("Text", function () {
return this. textcontent
});
Define selectSingleNode, 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.documentelement);
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.