【发布时间】:2013-12-18 12:49:30
【问题描述】:
这是我的html:
<html><body>
page starts here
this is a test page
top:
<script id="invoc_code" type="text/javascript">
window.onload = function() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function() {
var body = document.body;
if (xhr.readyState == 4 && xhr.status == 200) {
if(body.firstChild){
body.insertBefore(xhr.response, body.firstChild);
} else {
body.appendChild(xhr.response);
}
}
};
xhr.open("GET", "http://google.com", true);
xhr.send();
};
</script>;
page ends here
</body></html>
body.insertBefore(xhr.response, body.firstChild); 行导致“NotFoundError:尝试在不存在的上下文中引用节点”。你能告诉我是什么原因吗?
提前致谢
【问题讨论】:
-
您只能将 DOMNodes 插入 DOM。我怀疑
xhr.response是一个。 -
我猜,body没有任何子节点
-
正确的 Marcell Fulop,谢谢:)
-
anand4tech ,有文本节点作为body的子节点。
标签: javascript