开发者

How do i run a js function from an iframe in the main frame?

开发者 https://www.devze.com 2023-02-19 08:19 出处:网络
In my iframe i have a function: function CheckDebug(){ if($(\'#debug\').length > 0){ debugConsole.debugOn = true;

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."

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号