问题描述
我正在尝试使用javascript从我的框架之一访问html文档,但是却遇到了未捕获的DOMException:阻止源为"null"的框架访问跨域框架.
错误.
I'm trying to access html document from one of my frames with javascript, but I'm getting a 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".
As you can see, I'm trying to extract the div
's content which is: "abc".
我认为两者: iframe.contentDocument:iframe.contentWindow.document
为null,我不知道为什么.
I think that both: iframe.contentDocument : iframe.contentWindow.document
are null and I don't know why.
推荐答案
如果您尝试直接从浏览器访问框架而没有Web服务,则出于安全原因,您将被禁止访问(您可以访问系统文件).
It looks like if you're trying to access frames directly from your browser without having a web service, you're not allowed due to security reasons (you could access system files).
我通过安装xampp解决了我的问题,并将我所有的文件都移到了htdocs中,一切正常.
I have soled my issue by installing xampp and moved all my files to htdocs and everything worked as expected.
这篇关于无法获取帧内容,未捕获DOMException:阻止了源为"null"的帧.访问跨域框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!