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

时间:2014-04-04 03:52:19

标签: 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>

1 个答案:

答案 0 :(得分:1)

所有弹出式窗口都具有相同的名称(window.open(url, 'Hints', ...)),这可能会使某些浏览器感到困惑,因此他们会认识到其他窗口是他们的开场白。

只是一个旁注,检测窗口是否有blur方法是不必要的,只需Hints.focus()

相关问题