How do i run a js function from an iframe in the main frame?
In my iframe i have a function:
function CheckDebug(){
if($('#debug').length > 0){
debugConsole.debugOn = true;
de开发者_如何学CbugConsole.IP = "<?=$_SERVER['REMOTE_ADDR']?>";
debugConsole.log('START DEBUG MODE');
}
}
How do i run that function in the main frame so that it affects the iframe's debugConsole
object?
To access the parent iframe use window.parent, you can call:
window.parent
So you would do:
window.parent.debugConsole(..);
as an example.
To have the parent access the iframe, you can do the following:
document.getElementById('frameId').contentWindow.funcThatLivesInIframe();
where the iframe has an id attribute with the value 'frameId'.
Just access the "top" context:
function CheckDebug(){
if (top.$('#debug').length > 0){
top.debugConsole.debugOn = true;
top.debugConsole.IP = "<?=$_SERVER['REMOTE_ADDR']?>";
top.debugConsole.log('START DEBUG MODE');
}
}
Now that's for the top-most window. If you just want to go up one level, you can look at "window.parent."
精彩评论