1

My question is simple, suppose i have a html file like this named goal.html:

<html>
.
.
.
<body>
...
<iframe id="main_display" src="../pages/main.html" width=300 height=300><iframe> 
.
.
.

and now i wrote a line of javascript in main.html like window.a ="a"

now my question is how can i get the value of window.a when i am in goal.html

after searching something and spending some time in google developing tool, i founded something relevant it was iframe.contentWindow it returns an object but when i try to open(i mean to expand) it, it shows me an error:

Uncaught [object DOMException]

So how do i do this thing?

Sanmveg saini
  • 744
  • 1
  • 7
  • 22

2 Answers2

0

this should work on goat.html

var childValue = document.getElementById("main_display").contentWindow.a;
cylua2
  • 180
  • 7
  • 1
    gave me an error: `Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.` – Sanmveg saini Sep 22 '15 at 15:34
  • 1
    the iframe url you are trying to access is on a different domain? due to [Same Origin Security Policy](https://en.wikipedia.org/wiki/Same-origin_policy), any browser blocks any script trying to access a frame that has another origin. – cylua2 Sep 22 '15 at 15:40
0

It's weird that it doesn't work on Chrome. I suggest you try to put it in .ready and .load function.

$(function () {  
    $("#myiframe").load(function () {                        
        frames["myframe"].document.body.innerHTML = htmlValue;
    });
});

For more detail, take a look at this answer.

Community
  • 1
  • 1
Triet Doan
  • 11,455
  • 8
  • 36
  • 69