Possible to call a Javascript method in the context of another window?

前端 未结 2 1985
予麋鹿
予麋鹿 2020-12-19 02:49

Say you have a global function alert2:

function alert2(msg) {
    window.alert(msg);
}

And you also have a reference to a second window obj

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 03:15

    You can use childWindow.opener to get the window object that opened the child window.

    alert2.call(childWindow.opener, "called from child using parent as context");
    

    Demo: http://jsfiddle.net/hJ7uw/8/

提交回复
热议问题