9

I set an iframe on my page and use below script to remove the double navigation bars. It works well on any browser, but doesn't work on Chrome, it shows double vertical navigation bars!

function calcHeight() {
    //find the height of the internal page
    var the_height = document.getElementById('the_iframe').contentWindow.
                     document.body.scrollHeight;

    //change the height of the iframe
    document.getElementById('the_iframe').height = (the_height + 30) +"px";
}

I get 2 errors messages in Chrome:

  • First error message:

    Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

    and it points out the error at:

    var the_height = document.getElementById('the_iframe').contentWindow.
                     document.body.scrollHeight;
    
  • Second error message, it is located in jquery-1.10.2.js:

    Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

    It points out the error message at:

    elem.contentDocument || elem.contentWindow.document : 
    
abcid d
  • 2,821
  • 7
  • 31
  • 46
  • This is because of same origin policy. You should include the location of that page and the one of the page in the iframe. – Oriol Sep 17 '14 at 14:17
  • Thank you, Oriol! The first error is from my page, the second one is from jquery-1.10.2.js. Would you please direct more about "should include the location of that page and the one of the page in the iframe." – abcid d Sep 17 '14 at 17:33
  • I meant you should say the URL of your page (e.g http://example.com/path/file.php) and the URL of the document loaded inside the `iframe` (i.e ``). This way we will see if they have the same origin or not. – Oriol Sep 17 '14 at 18:52
  • Thank you, Oriol! My page is: JSExample.html, this page contains an iframe: . These 2 pages (JSExample.html and JS1.html) are in the same folder and the same level. – abcid d Sep 17 '14 at 22:01
  • 1
    Then it seems it should work. Are those pages local? In that case, see [Using iframe with local files in Chrome](http://stackoverflow.com/q/17950598/1529630) – Oriol Sep 18 '14 at 14:16

2 Answers2

1

Try setting up a server on your machine and test your page from there, instead of on your local file system.

If you have Python 2, do python -m SimpleHTTPServer [port]
In Python 3, do python -m http.server [port]
This will set up a server on localhost:[port]. Then fire up a browser and navigate to your page and see if the problem goes away.

Yibo Yang
  • 2,353
  • 4
  • 27
  • 40
-4

You can try to add document.domain = 'yourdomain.com' to you page

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
user3559718
  • 81
  • 1
  • 4