javascript - Chrome 和 Firefox 相当于 IE 的 window.opener.functionName()

标签 javascript html google-chrome firefox

我在 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/

相关文章:

javascript - 使用 JQuery Mobile ui-datepicker 保存日期/事件

html - 使用属性缩放不好吗?

javascript - 目标 =“_blank” 在 Chrome 中不起作用?

javascript - 异步文件夹创建有什么意义?

javascript - 如何从不同域的脚本报告 JS 错误?

javascript - scrollTop 等于 0,即使 div 位于页面下方

html - 更改子导航的 css,同时保留带有图片的原始导航

jquery - 在鼠标移出时更改颜色 'a' 标签

jQuery;对于 new Image(),Chrome 图像宽度和高度 = 0

javascript - Cordova OpenTok 集成 Chrome 问题