admin管理员组

文章数量:1024194

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.

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.

本文标签: