我在 IE 中使用 window.opener.functionName() 从子窗口调用父函数,它工作得很好。然而,同样的方法在 Chrome/Firefox 中不起作用。
我尝试了 window.top.functionName(); Parent.window.top.functionName() 和许多其他的。没有人在工作。
谁能帮忙!
编辑
这是代码。
请注意,我有 2 级层次结构。我需要从 ChildCall2.jsp 文件调用 Parent.jsp 的 updateHTML() 函数
Parent.jsp
<!DOCTYPE html>
<html>
<head>
<TITLE>Parent function call test</TITLE>
<script>
function openwindow(url)
{
Hints=window.open(url, 'Hints', "resizable=yes,scrollbars=yes,width=475,height=225");
if(Hints.blur)
Hints.focus();
}
function updateHTML()
{
alert("Parent called successfully");
}
</script>
</head>
<body onFocus="">
<a href="javascript:openwindow('ChildCall.jsp')"> Click Me </a>
</body>
</html>
ChildCall.jsp
<!DOCTYPE html>
<html>
<head>
<TITLE>Child function call test</TITLE>
<script>
function openwindow(url)
{
Hints=window.open(url, 'Hints', "resizable=yes,scrollbars=yes,width=475,height=225");
if(Hints.blur)
Hints.focus();
}
function updateHTML1()
{
alert("Parent call function");
window.opener.updateHTML();
}
</script>
</head>
<body onFocus="">
<a href="javascript:openwindow('ChildCall2.jsp')"> Click Me </a>
</body>
</html>
ChildCall2.jsp
<!DOCTYPE html>
<html>
<head>
<TITLE>Child function2 call test</TITLE>
<script>
function openwindow(url)
{
Hints=window.open(url, 'Hints', "resizable=yes,scrollbars=yes,width=475,height=225");
if(Hints.blur)
Hints.focus();
}
function updateHTML2()
{
alert("Parent call function2");
window.opener.updateHTML1();
}
</script>
</head>
<body onFocus="">
<a href="javascript:updateHTML2()"> Click Me to call parent function </a>
</body>
</html>
最佳答案
所有弹出窗口都具有相同的名称(window.open(url, 'Hints', ...))
,这可能会使某些浏览器感到困惑,因此它们会识别其他窗口作为其打开窗口.
顺便说一句,检测窗口是否有 blur
方法是不必要的,只需执行 Hints.focus()
即可。
关于javascript - Chrome 和 Firefox 相当于 IE 的 window.opener.functionName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22853197/