5

I have a page with an iframe that contains a html page. I want to access a Javascript variable in the parent page from within the iframe. The name of the variable in the main page is observer.

I have tried this

parent.observer = 'aadasds'; 

but I am getting the following error:

Permission denied for to get property Window.observer from

.

3
  • Can we see some code / a link? Commented Oct 7, 2010 at 22:44
  • 2
    A code example would be helpful. However, I believe you can only access a variable in the parent page if it's from the same domain. If the iframe exists on a different domain than the parent, that may be the source of your problem. Commented Oct 7, 2010 at 23:03
  • dyn-web.com/tutorials/iframes Commented Oct 8, 2010 at 2:56

2 Answers 2

4

Exchanging values between iframes (and parent) is only allowed if both sites come from the same domain. If they do, your example should just work. If they don't, browsers inhibit the communication.

However there are a number of hacks to circumvent this: e.g the Yahoo.CrossFrame library described in Julien le Comte's blog using a third iframe to enable one way communication, or the "resize an iframe around the iframe"-idea described in Adam Fortuna's blog enabling two way communication.

Edit (as people still seem to read this old answer):
In modern Browsers you can use postMessage to exchange Data between iframes. There are many javascript libraries that try to emulate that functionality in older browsers, too. E.g. by mis-using the location.hash, like the jquery-postmessage-plugin does.

Sign up to request clarification or add additional context in comments.

3 Comments

"Exchanging values between iframes (and parent) is only allowed if both sites come from the same domain. If they do, your example should just work. If they don't, browsers inhibit the communication." - can a javascript function on the page be called from the page inside the iframe?
@Niall Collins: Yes, without problems - as long as both pages come from the same domain. The error message you describe only appears if they originate from different domains, as far as I know.
How would you answer the question if the iframes where the same domain?
2

It sounds as though your iframes are using different domains. All major browsers block access to parent iframes if they are not using the same domain. IE if you have the domain www.test.com and you embedded a page from www.google.com and try to access/modify anything from google's website, you will be denied access.

The other answer to this question explains the implemented API post message. This can also be used to send/receive data from different frames from different domains. However, the things you can do with that are limited compared to if you had two frames using the same domain.

That being said, here is the answer if your iframes are using the same domain.

window.parent.observer; 

Hope this helps someone :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.