如何从子窗口调用父级的父 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
How to call parent's parent javascript function from child window
提问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.opener
might 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.opener
is a reference to the window that opened the current window, then I guess you should be able to call the opener
property 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.top
instead:
如果你想到达最顶层的窗口(根窗口,或者你称之为的窗口),那么你可以使用window.top
:
window.top.abc();