【问题标题】:window.opener.location.href works in IE but not Chrome or Safariwindow.opener.location.href 适用于 IE,但不适用于 Chrome 或 Safari
【发布时间】:2011-10-11 00:09:23
【问题描述】:

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

我有一个应用程序已成功使用以下功能在完成弹出窗口后重定向回父窗口。最近在研究其他浏览器的兼容性(让系统可以通过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)或 Chrome 中无效。

有什么想法吗?

【问题讨论】:

  • 如果用户在此期间关闭了开启器,这会有所帮助,例如:if (window.opener) { window.opener.document.location.href = url;} else { /*opener is closed, deal with it*/ }
  • 谢谢罗伯。是的,一旦我让功能在没有它的情况下工作,肯定会这样做。谢谢。

标签: javascript google-chrome safari window.opener


【解决方案1】:

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

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

这也适用于 IE。它也是一个属性,即使它允许您将其用作方法。

【讨论】:

  • 啊,是的,知道这很简单。现在工作。谢谢古法。
  • @Barbs 如果这是正确答案,您应该接受。点击大勾号。
【解决方案2】:

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

家长:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多