Get URI of XML DOM Object Using Internet Explorer

In Firefox and Chrome, the documentURI property of the XML DOM object's documentURI object node will return the DOM URI if it was created using the XMLHTTPRequest object.

Is there an equivalent property for the Internet Explorer DOM, and if so, what is it? The documentURI , url , url and baseURI return either null or undefined.

The MSXML documentation for the url property made me hope that this would return the URL used in the HTTP request that the DOM created - but the XMLHTTPRequest is not used in the above example.

The code I used to create the DOM, and then check the property below:

 function getXslDom(url) { if (typeof XMLHttpRequest == "undefined") { XMLHttpRequest = function () { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }; } var req = new XMLHttpRequest(); req.open("GET", url, false); req.send(null); var status = req.status; if (status == 200 || status == 0) { return req.responseXML; } else { throw "HTTP request for " + url + " failed with status code: " + status; } }; var xslDom = getXslDom('help.xsl'); // the following shows "undefined" for IE window.alert(xslDom.documentURI); 
+4
source share
1 answer

Using the example from the MSXML page that you linked, I managed to get it to work:

 <script> var getXslDom = function(url) { if(typeof ActiveXObject === 'function') { var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0"); xmlDoc.async = false; xmlDoc.load(url); if (xmlDoc.parseError.errorCode != 0) { var myErr = xmlDoc.parseError; throw "You have error " + myErr.reason; } else { return xmlDoc; } } else { var req = new XMLHttpRequest(); req.open("GET", url, false); req.send(null); var status = req.status; if (status == 200 || status == 0) { return req.responseXML; } else { throw "HTTP request for " + url + " failed with status code: " + status; } } } var dom = getXslDom('help.xsl') alert(dom.documentURI || dom.url) </script> 

Here is a demo .

Hooray!

PS: I used "alert" only because the OP seems to use it, personally I prefer "console.log", which I also recommend OP.

0
source

All Articles