如何使用window.open()专注于先前打开的窗口 [英] How to focus on a previously opened window using window.open()

查看:200
本文介绍了如何使用window.open()专注于先前打开的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为网站打开一个弹出窗口,并保留该窗口,以便音乐播放器在用户浏览页面时能够播放无缝音乐.

I would like to open a popup window for a website and keep it for a music player to be able to play seamless music while the user is navigating across pages.

我想要实现的是,当用户首先单击音乐"按钮时,会使用window.open(完成)打开一个新的弹出窗口.第二部分是当用户导航到其他页面并再次单击音乐"按钮时,已经打开的窗口会收到焦点,但不会再次加载.

What I would like to achieve is that when the user first clicks the "Music" button a new popup window opens using window.open (done). The second part would be that when the user navigates to other pages and clicks again on the "Music" button then the already open window receives the focus but doesn't load again.

但是,一旦用户离开原始页面,浏览器就会忘记该窗口已打开并且focus()停止工作.似乎focus()可以在用户停留在同一页面上的范围内发挥作用.

However what is happening is that as soon as the user navigates away from the original page then the browser forgets that the window has been opened and the focus() stops working. It seems that focus() works as far as the user is staying on the same page.

我发现了一个类似的问题,并试图根据该问题实现我的代码:

I have found a similar question and tried to implement my code based on that question:

JavaScript窗口.仅当窗口不存在

这是我到目前为止编写的代码:

Here is the code I have written so far:

$('a.music').click(function(){
        if(typeof(winRef) == 'undefined' || winRef.closed){
            winRef = window.open(this.href,'Music','left=20,top=20,width=500,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,titlebar=0');
        } else {
            winRef.focus();
        }
        return false;
    });

如果您想看看,这里是现场站点. http://ilhaamproject.com/

Here is the live site if you would like to have a look. http://ilhaamproject.com/

我应如何引用已打开的窗口,以使其可在不同的页面加载中使用?

推荐答案

编辑:我拿回了我以前的答案.可以做到的.只需在打开的窗口上将打开的窗口设置为winRef即可.在弹出窗口中,输入以下代码:

I take my earlier answer back. This can be done. Just have the opened window set winRef on its opener. In your popup window, put this code:

setInterval(function()
{
    opener.winRef = window;
}, 500);

就是这样!

可能会有一些事件可以挂在打开器窗口上,因此您不必使用setInterval,但是我不确定.

There may be some event you can hook on the opener window so you don't have to use the setInterval, but I'm not sure.

原始答案:

您不能-卸载脚本页面后,JavaScript变量消失了.唯一的方法是不卸载打开弹出窗口的页面.您可以保持页面打开,但可以使用AJAX更改其内容.或者,您可以使用我建议的帧技术进行类似操作上周的问题.

You cannot - JavaScript variables are gone after the script's page is unloaded. The only way you could do this would be to not unload the page that opened the popup. You can keep the page open, but change its content using AJAX. Or you can use the frames technique I suggested for a similar question last week.

这篇关于如何使用window.open()专注于先前打开的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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