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

I'm trying to access html document from one of my frames with javascript, but I'm getting a U

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745518661a4631155.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信