最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to get frame content, Uncaught DOMException: Blocked a frame with origin "null" from acces

programmeradmin1浏览0评论

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.

This is the main page:

<!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>

And this is the frame:

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>

<div id="div-content">
    abc
</div>

</body>
</html>

As you can see, I'm trying to extract the div's content which is: "abc".

I think that both: iframe.contentDocument : iframe.contentWindow.document are null and I don't know why.

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.

This is the main page:

<!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>

And this is the frame:

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>

<div id="div-content">
    abc
</div>

</body>
</html>

As you can see, I'm trying to extract the div's content which is: "abc".

I think that both: iframe.contentDocument : iframe.contentWindow.document are null and I don't know why.

Share Improve this question asked Jun 19, 2018 at 19:12 Zbarcea ChristianZbarcea Christian 9,54822 gold badges88 silver badges140 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

It looks like if you're trying to access frames directly from your browser without having a web service, you're disallowed due to security reasons (you could access system files).

I solved my issue by installing xampp and moving all my files to htdocs and everything worked as expected.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论