window.opener.location.href适用于IE,但不适用Chrome或Safari

window.opener.location.href works in IE but not Chrome or Safari

本文关键字:不适用 Chrome Safari IE opener location href 适用于 window      更新时间:2023-09-26

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

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

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

Safari中的控制台报告"window.open .location的结果"。Href"不是函数"。我尝试使用上述和"window.opener.document.location"。Href '和'window.open .window.location. '。(从网上提供的其他解决方案中提取),但没有成功。

我知道有些人有这个功能工作得很好,而其他人有这种问题。我想知道对于这种特殊情况是否有什么答案。

下面是我的函数:

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

这在IE7,8和9上从第一天开始工作,但在Safari (windows或iPad)或Chrome上不起作用。

任何想法?

href是一个属性,不是一个方法。把URL赋给它:

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

这也将在IE中工作。它也是一个属性,即使它允许你把它作为一个方法使用

使用下面的代码来达到预期的效果。

父:

<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>