我正在尝试使用 javascript 从我的一个框架访问 html 文档,但我得到了Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.
error.
这是主页:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>
(function(window, document, undefined){
window.onload = init;
function init(){
var iframe = document.getElementById('main_frame');
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
var ulObj = innerDoc.getElementById("div-content");
console.log(ulObj.innerHTML);
}
})(window, document, undefined);
</script>
</head>
<frameset rows="*" noresize="noresize" border="0" frameborder="no" framespacing="0">
<frame src="frame.html" id="main_frame" frameborder="no" marginheight="0" marginwidth="0">
</frameset>
</html>
这是框架:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="div-content">
abc
</div>
</body>
</html>
正如你所看到的,我正在尝试提取div
的内容是:“abc”。
我认为两者:iframe.contentDocument : iframe.contentWindow.document
为空,我不知道为什么。
看起来,如果您尝试在没有 Web 服务的情况下直接从浏览器访问框架,则会因安全原因而被禁止(您可以访问系统文件)。
我通过安装 xampp 并将所有文件移至 htdocs 解决了我的问题,一切都按预期进行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)