简体   繁体   English

Javascript-在一页上打开窗口,然后在另一页上关闭

[英]Javascript - open window on one page, then close it on another

Let me explain my scenario. 让我解释一下我的情况。 I have a play/stop pair of CSS buttons at my footer. 我的页脚有一对CSS播放/停止按钮。 I don't want to use frames. 我不想使用框架。 When I press play on the current page (page 1), a popup opens and jplayer starts playing music. 当我在当前页面(第1页)上按播放时,将弹出一个弹出窗口,并且jplayer开始播放音乐。 When I press close, the window closes thanks to my persisting the window value from the initial opening. 当我按下关闭按钮时,由于我保留了最初打开时的窗口值,因此窗口关闭了。

Now, here's my problem: Lets say the user moves to another page (page 2) on the main site - the popup remains playing (that's not a problem, that's what I intended). 现在,这是我的问题:假设用户移至主站点上的另一个页面(第2页)-弹出窗口仍在播放(这不是问题,这就是我的意图)。 But I would like for the user to be able to press stop on page 2 and kill the window that was created in page 1. 但我希望用户能够按第2页上的Stop终止在第1页上创建的窗口。

I tried using HTML5 local storage, but it didn't work - odd thing: the object stored in HTML5 storage, is the of type window - so it is stored. 我尝试使用HTML5本地存储,但是没有用-奇怪的是:HTML5存储中存储的对象是窗口类型的-因此将其存储。 It still seems unable to close it. 似乎仍然无法关闭它。

Has anyone ever done this before or know of a way to get around it? 有没有人做过此事,或者知道解决方法? Here is my code (which does not work) as is: 这是我的代码(无效):

$(document).ready(function () {
    var win;

    $('#playButton').click(function () {                                
        win = window.open('Player.html', 
                          "popupWindow", 
                          "width=265,height=360,scrollbars=yes");
        $(this).attr('class', 'active');
        $('#stopButton').attr('class', 'none');
        localStorage.setItem("player", win);
    });

    $('#stopButton').click(function () {
        $(this).attr('class', 'active');
        $('#playButton').attr('class', 'none');                                    
        win = localStorage.getItem("player");
        win.close();
    });                                 
});

Following my comment: Change your page's code to: 按照我的评论:将您的页面代码更改为:

$(document).ready(function () {
                            $('#playButton').click(function () {                                
                                window.open('Player.html', "popupWindow", "width=265,height=360,scrollbars=yes");
                                $(this).attr('class', 'active');
                                $('#stopButton').attr('class', 'none');
                            });

                            $('#stopButton').click(function () {
                                $(this).attr('class', 'active');
                                $('#playButton').attr('class', 'none');                                    
                                localStorage.setItem("player", false);
                            });                                
                        });

And add this to your popup's code: 并将其添加到弹出窗口的代码中:

 setInterval(function(){
   if(localStorage.getItem("player") == false)
     window.close();
 }, 1000);

SetInterval will run the internal anonymous function every 1000ms, and it checks the player value and will close itself if it is set to false. SetInterval将每隔1000ms运行一次内部匿名函数,它会检查播放器值,如果将其设置为false,则会自行关闭。

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

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