gpt4 book ai didi

javascript - 使用 Internet Explorer 检索 XML DOM 对象的 URI

转载 作者:数据小太阳 更新时间:2023-10-29 02:14:08 25 4
gpt4 key购买 nike

在 Firefox 和 Chrome 中,XML DOM 的文档节点对象的 documentURI 属性将返回 DOM 的 URI,如果它是使用 XMLHTTPRequest 对象创建的。

Internet Explorer DOM 是否有等效的属性?如果有,那是什么? documentURIurlURLbaseURI 属性都返回 null 或 undefined。

MSXML documentation对于 url 属性,我希望这会返回创建 DOM 的 HTTP 请求中使用的 URL - 但给出的示例没有使用 XMLHTTPRequest.

我用来创建 DOM 然后测试属性的代码如下:

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

最佳答案

使用您链接的 MSXML 页面中的示例,我设法让它工作:

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

这里是 a demo .

干杯!

PS:我使用“alert”只是因为 OP 似乎使用它,我个人更喜欢“console.log”,我也向 OP 推荐它。

关于javascript - 使用 Internet Explorer 检索 XML DOM 对象的 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com