Iframe accessing variables

Hey

is it possible to access javascript variables in an iframe src page from the parent page, or vice versa. How do you do this? What about the DOM?

thanks

If they both come from the same origin (ie the same domain), then each can access the other’s variables as normal.

To access the parent’s scope from the child you can use window.parent and to access the child’s scope from the parent you can use a couple of methods, such as [url=https://developer.mozilla.org/en/DOM/window.frames]window.frames.

So, for instance, if you define a var myvar = 2 in the parent scope, you can access that in the iframe as window.parent.myvar.

If the page inside the iframe comes from a different domain to the parent page then it cannot access anything from the parent (with only a very small number of exceptions, such as being able to reload/redirect the page). This same is mostly true of the parent page accessing anything from the child frame as well.