在chrome的新选项卡中查找由window.open先前打开的窗口 [英] Find window previously opened by window.open in new tab in chrome

查看:949
本文介绍了在chrome的新选项卡中查找由window.open先前打开的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我有以下代码(来自相关的),用于查找以前打开的窗口的引用并关闭它们。 var playerUrl ='http://my.player ...';
var popupPlayer = window.open('','popupPlayer','width = 150,height = 100');
if(popupPlayer.location =='about:blank'){
popupPlayer.location = playerUrl;
}
popupPlayer.focus();

这也适用于firefox中的标签,但是在chrome中,好像每个单独的标签都不知道在其他标签中发生了什么。我怀疑这是由于每个标签是一个不同的过程,但无论如何绕过这个?

所以基本上,如果我们打开窗口A从页面A在标签A,我们可以从B页面的B页面获得对A窗口的引用,然后用chrome关闭它吗?



相关:



编辑:

对于发送所有其他选项卡来执行关闭操作非常有用,尽管未在Chrome中进行测试:

已经用过了jStorage的PubSub功能之前要做到这一点。它依赖于jQuery,所以如果你还没有使用它,它可能不是最好的解决方案,但你可以看看源代码看看它是如何工作的。



在所有选项卡和弹出窗口中包含jStorage。在弹出窗口中,添加以下代码:

  $。jStorage.listenKeyChange('fireClose',function(){
window.close()
});

然后,当您想从任何标签关闭它时:

  $。jStorage.set('fireClose','这里任何东西,为了简单起见'); 

您可以使用第二个参数实际发送数据到其他窗口,如果您想要更多复杂的交互,但是在这种情况下,只需通过 jStorage.set 更改密钥就足够了。



http://www.jstorage.info/


I have the following code (from related) that works for finding a reference to previously opened windows and closing them.

var playerUrl = 'http://my.player...';
var popupPlayer= window.open('', 'popupPlayer', 'width=150,height=100') ;
if(popupPlayer.location == 'about:blank' ){
    popupPlayer.location = playerUrl ;
}
popupPlayer.focus();

This also works across tabs in firefox, but in chrome it seems as if each separate tab is unaware of what is happening in the other tabs. I'd suspect this is due to each tab being a different process but is there anyway to bypass this?

So basically, if we open window A from page A in tab A, can we get a reference to window A from page B in tab B, and close it in chrome?

Related to:

Edit:

I found this that may be useful for messaging all other tabs to do the close action, though haven't tested in chrome: Sending a message to all open windows/tabs using JavaScript

解决方案

I've used the PubSub functionality of jStorage before to do this. It's dependent on jQuery, so it may not be the best solution if you're not already using it, but you can check out the source code to see how it works.

Include jStorage on all tabs and the popup windows. On the popup windows, add this code:

$.jStorage.listenKeyChange('fireClose', function(){
    window.close()
});

Then, when you want to close it from any tab:

$.jStorage.set('fireClose', 'anything here, for simplicity');

You can use the second parameter to actually send data to the other windows if you want to have some more complicated interactions, but simply changing the key through jStorage.set is sufficient in this case.

http://www.jstorage.info/

这篇关于在chrome的新选项卡中查找由window.open先前打开的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆