简体   繁体   English

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

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

I have been researching this problem and while there's lots of posts on various forums about similar problems, none of the problems or solutions exactly match mine.我一直在研究这个问题,虽然在各种论坛上有很多关于类似问题的帖子,但没有一个问题或解决方案与我的完全匹配。

I have an application that has been successfully using the function below to redirect back to the parent window once finished with a popup.我有一个应用程序已成功使用下面的函数在完成弹出窗口后重定向回父窗口。 Recently I have been investigating compatibility with other browsers (to allow the system to be used via iPad) and have found that there is a problem with this function when using Safari or Chrome.最近我一直在研究与其他浏览器的兼容性(允许系统通过iPad使用),发现在使用Safari或Chrome时该功能存在问题。

The parent page is a summary of some databased info, and the user clicks a link to open a window (via window.open) to view more detailed data.父页面是一些数据库信息的摘要,用户单击链接打开一个窗口(通过 window.open)以查看更详细的数据。 When finished there is a link on the child window that refreshes the data on the parent (partly to ensure the correct data is displayed when you return to the parent) and closes the child.完成后,子窗口上有一个链接可以刷新父窗口上的数据(部分是为了确保返回父窗口时显示正确的数据)并关闭子窗口。

The Console in Safari reports "the result of 'window.opener.location.href' is not a function". Safari 中的控制台报告“'window.opener.location.href' 的结果不是函数”。 I have tried to use the above and 'window.opener.document.location.href' and 'window.opener.window.location.href' (taken from other solutions offered up on the net) but with no success.我曾尝试使用上面的“window.opener.document.location.href”和“window.opener.window.location.href”(取自网上提供的其他解决方案),但没有成功。

I know that some people have this function working well, while others have this sort of problem.我知道有些人的这个功能运行良好,而其他人则有这种问题。 I'm wondering if there are any answers to this specific situation.我想知道这种特定情况是否有任何答案。

Here is my function:这是我的功能:

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

This has worked from day one on IE7,8 and 9 but doesn't work in Safari (for windows or iPad) or Chrome.这从第一天开始就在 IE7、8 和 9 上起作用,但在 Safari(适用于 Windows 或 iPad)或 Chrome 中不起作用。

Any ideas?有任何想法吗?

href is a property, not a method. href是一个属性,而不是一个方法。 Just assign the URL to it:只需将 URL 分配给它:

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

This will work in IE also.这也适用于 IE。 It's a property there too, even if it lets you use it as a method.它也是那里的一个属性,即使它允许您将其用作方法。

Use below code to achieve desired results.使用下面的代码来达到预期的结果。

Parent:家长:

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

Popup:弹出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM