javascripthtmlframenul

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


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.


Solution

  • 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.