Access variable on parent page from iframe

I have an iframe page that contains an html page. I want to access the Javascript variable on the parent page from an iframe. The name of the variable on the observer main page.

I tried this

 parent.observer = 'aadasds'; 

but I get the following error:

Allowed permission to get Window.observer property from

.

+4
source share
2 answers

The exchange of values ​​between iframes (and the parent) is allowed only if both sites belong to the same domain. If so, your example should work. If they do not, browsers prohibit communication.

However, there are a few hacks to get around this: for example, the Yahoo.CrossFrame library described on Julien le Comte’s blog , using the third iframe to enable one-way communication or the “resize iframe around iframe” -idea described on Adam Fortune’s blog , allowing two-way communication .

Edit (as people still seem to be reading this old answer):
In modern browsers, you can use postMessage to exchange data between iframes. There are many javascript libraries that also try to emulate this function in older browsers. For instance. by misusing location.hash, e.g. jquery-postmessage-plugin .

+3
source

It sounds like your frames use different domains. All major browsers block access to the parent iframe if they do not use the same domain. IE, if you have a domain www.test.com , and you entered a page from www.google.com and try to access / change something from the google website, you will be denied access.

Another answer to this question explains the implemented post message API. It can also be used to send / receive data from different frames from different domains. However, what you can do with this is limited compared to the fact that you had two frames using the same domain.

As the saying goes, here is the answer if your frames use the same domain.

 window.parent.observer; 

Hope this helps someone :)

+2
source

All Articles