【问题标题】:Unable to get frame content, Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame无法获取框架内容,未捕获的 DOMException:阻止具有源“null”的框架访问跨域框架
【发布时间】: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


    【解决方案1】:

    看起来如果您尝试直接从浏览器访问框架而不使用 Web 服务,由于安全原因(您可以访问系统文件),您会被禁止。

    我通过安装 xampp 并将我的所有文件移动到 htdocs 解决了我的问题,一切都按预期工作。

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 2018-12-14
      • 2019-01-19
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多