【发布时间】:2018-11-28 20:00:18
【问题描述】:
我正在尝试使用 javascript 从我的一个框架中访问 html 文档,但我收到了 Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame. 错误。
这是主页:
<!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 都是空的,我不知道为什么。
【问题讨论】:
标签: javascript html frame nul