【问题标题】:window.opener | not working in chrome开窗器 |不能在铬中工作
【发布时间】:2014-02-10 07:22:53
【问题描述】:

我有两个 html 页面..我正在从子级调用父窗口..一切都很好。但是在 Chrome 中它失败了..让我知道原因..

test1.html :-

<html>
<head>
<title>Compose</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<SCRIPT>
function test(){
//alert('');
var win = window.open('../login/test2.html',"","height=700,width=800");
}
function test1(){
alert('test1');
}
</SCRIPT>
</head>
<body>

<input type="button" value="click" onclick="test();" />             
</body>
</html>

test2.html :-

<html>
<head>
<title></title>
<SCRIPT>
function opener1(){
try{
    if(window.opener != null && !window.opener.closed)
    {

    }
    window.opener.test1();
    }catch(e){ alert(e.description);}
}
</SCRIPT>
</head>
<body oncontextmenu="return false"  ondragstart="return false" onload="opener1();">
<h1>Test Page</h1>

</body>
</html>

从 test2.html 调用 test1.html 中的方法不起作用..任何解决方案..赞赏..谢谢

【问题讨论】:

    标签: javascript html google-chrome


    【解决方案1】:

    只能使用parent 变量访问父窗口。 以下对 opener1 函数的修改应该可以实现这一点

    function opener1(){
        try{
            if(parent.window.opener != null && !parent.window.opener.closed)
            {
              parent.window.opener.test1();
            }
    
        }catch(e){ alert(e.description);}       
    }
    

    【讨论】:

    • 为什么parent 会参与其中?
    • 嗨,anurpur 还是一样的 pblm..alert 说未定义
    • 因为您想访问调用第二个窗口(test2.html)的窗口(test1.html)的函数。所以 test1.html 是 test2.html 的父窗口。这就是javascript的运作方式。简单地调用window就是指当前窗口。
    • 如果警报显示 undefined ,将调用 catch 块中的警报。您可以粘贴控制台中显示的错误吗?
    • 有趣..这方面似乎没问题。您是否 100% 确定您对 test2.html 中的 opener1 函数进行了更改?
    【解决方案2】:

    代码对我来说似乎很好。如果您发现异常,您可以将其记录到控制台以获取错误信息。确保 test1.html 和 test2.html 的域匹配,否则会出现安全异常。

    【讨论】:

    • error msg is :blocked a frame with origin "null" from access a cross origin frame
    • 所以这意味着域与您的父子窗口不匹配。如果从文件系统打开 html,这也不起作用。
    【解决方案3】:

    你可以试试我的代码:

    window.opener.document.location.href = url;
    

    这适用于 IE 和 Chrome。

    window.opener.location.href = 'test.html';
    

    这将打开页面“test.html”。

    您可以在父窗口打开后关闭它。

    如果我的答案有效,请不要忘记将我的答案标记为正确。

    【讨论】:

      【解决方案4】:

      这是因为windows 对象的opener 属性在iPad 或Chrome 等设备上为空。

      【讨论】:

      • 我在 Windows 7 下使用 Chrome 64 并且“window.opener”根本不为空。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2022-07-23
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      相关资源
      最近更新 更多