It is possible but only if the iframed page and its parent are loaded from the same domain... otherwise it's not allowed to access the parent form iframe and vice versa.
But let's assume that domains are the same...
Accessing parents variables (or functions)
define some container object in the window namespace, e.g.
Code:
window.sharedVars = {}
all the functions or variables you want to share put their references also to the window.exportVars, e.g.
Code:
function hello(){
alert("Hello iframe!");
}
window.sharedVars.sayHello = hello;
in the iframed page access the variables as following:
Code:
//call the parent's function
window.parent.sharedVars.sayHello();
//set a variable in parents namespace
window.parent.sharedVars.responseText = "Hello parent!";
Accessing iframed variables (or functions)
make sure your iframe has a name attribute (e.g. name="example-frame")
then you can access the iframe object by window.frames['example-frame']
same as in parent define some container object in the window namespace, e.g.
Code:
window.sharedVars = {}
all the functions or variables in your iframe you want to share with parent put their references also to the window.exportVars, e.g.
Code:
function iframedHello(){
alert("Hello parent!);
}
window.sharedVars.sayHello = iframedHello;
in the parent page access the variables as following:
Code:
//call the iframed function
window.frames['iframe-name'].sharedVars.iframedHello();
//set a variable in the iframe namespace
window.frames['iframe-name'].sharedVars.responseText = "Hello parent!";
NOTICE: This approach won't work if you open your test page from the file system. It will only work loaded from the server - remote or just form your localhost (e.g. localhost/test.html)
Bookmarks