如何从子窗口调用父级的父 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12581935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:34:48  来源:igfitidea点击:

How to call parent's parent javascript function from child window

javascript

提问by Beginner

How to call parent's parent javascript function from child window.

如何从子窗口调用父级的父 JavaScript 函数。

Example- Parent 1 has javascript function abc() Now how to call Parent1 Javascript function in Child window from Parent 2 which is initially trigged from Parent 1 Window. I tried window.parent.parent. Still no luck.

示例 - 父 1 具有 javascript 函数 abc() 现在如何从最初从父 1 窗口触发的父 2 中在子窗口中调用 Parent1 Javascript 函数。我试过window.parent.parent。仍然没有运气。

Thanks in advance

提前致谢

回答by Christofer Eliasson

Not sure what you mean with a child window. But I guess window.openermight be what you are looking for.

不确定您对子窗口的意思。但我想window.opener可能是你正在寻找的。

// Call abc in the window that opened the current window
window.opener.abc();

Update

更新

Haven't tried it, but since window.openeris a reference to the window that opened the current window, then I guess you should be able to call the openerproperty on that reference, to get a reference to the its parent as well:

还没有尝试过,但由于window.opener是对打开当前窗口的窗口的引用,那么我想您应该能够调用该opener引用上的属性,以获取对其父级的引用:

window.opener.opener.abc();

If you want to reach the topmost window (the root window, or what you would call it), then you can use window.topinstead:

如果你想到达最顶层的窗口(根窗口,或者你称之为的窗口),那么你可以使用window.top

window.top.abc();