无法获取帧内容,Uncaught DOMException:阻止具有原点“null”的帧访问跨源帧

问题描述 投票:0回答:1

我正在尝试使用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都是null,我不知道为什么。

javascript html frame nul
1个回答
0
投票

看起来如果您尝试直接从浏览器访问框架而没有Web服务,则由于安全原因(您可以访问系统文件)而不允许这样做。

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

© www.soinside.com 2019 - 2024. All rights reserved.