window.opener.location.href在IE中有效,但在Chrome或Safari中不起作用

问题描述 投票:6回答:2

我一直在研究这个问题,尽管在各个论坛上有很多关于类似问题的帖子,但这些问题或解决方案均与我的问题不完全匹配。

我有一个应用程序,一旦完成弹出窗口,该应用程序已成功使用下面的功能重定向回父窗口。最近,我一直在研究与其他浏览器的兼容性(以允许通过iPad使用该系统),并且发现在使用Safari或Chrome时此功能存在问题。

父页面是一些数据库信息的摘要,用户单击链接以打开一个窗口(通过window.open)以查看更详细的数据。完成后,子窗口上的链接将刷新父数据(部分是为了确保返回父时显示正确的数据)并关闭子。

Safari的控制台报告“'window.opener.location.href'的结果不是函数”。我尝试使用上面的“ window.opener.document.location.href”和“ window.opener.window.location.href”(取自网络上提供的其他解决方案),但没有成功。

我知道有些人的功能很好,而另一些人则有这种问题。我想知道是否对此特定情况有任何答案。

这是我的职能:

function quicklink(url) {
window.opener.document.location.href(url);
self.close();
}

这从IE7,8和9的第一天开始就起作用了,但是在Safari(适用于Windows或iPad的Safari)或Chrome中不起作用。

有什么想法吗?

javascript google-chrome safari window.opener
2个回答
20
投票

href是属性,而不是方法。只需为其分配URL:

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

这也将在IE中起作用。它也是那里的一个属性,即使它允许您将其用作方法。


0
投票

使用下面的代码获得所需的结果。

父母:

<script language="javascript">

function open_a_window() 
{
    var w = 200;
        var h = 200;
        var left = Number((screen.width/2)-(w/2));
        var tops = Number((screen.height/2)-(h/2));

        window.open("window_to_close.html", '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);

   return false;
}

// opener:
window.onmessage = function (e) {
  if (e.data === 'location') {
    window.location.replace('https://www.google.com');
  }
};

</script>

<input type="button" onclick="return open_a_window();" value="Open New Window/Tab" />

弹出窗口:

<!DOCTYPE html>
<html>
<body onload="quitBox('quit');">

<h1>The window closer:</h1>

<input type="button" onclick="return quitBox('quit');" value="Close This Window/Tab" /> 


<script language="javascript">

function quitBox(cmd) 
{      
    if (cmd=='quit')    
    {   
       window.opener.postMessage('location', '*');
       window.open(location, '_self').close();    
    }     
    return false;   
}

</script>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.