简体   繁体   English

如何使用javascript或jquery检查命名窗口是否存在

[英]How do I use javascript or jquery to check if named window exists

Here is the scenario. 这是场景。 I have a static audio player in the footer of a site. 我在网站的页脚中有一个静态音频播放器。 If you go to another page, the audio player reloads. 如果转到另一个页面,音频播放器将重新加载。 Not an issue because it's an audio stream not a static file. 没问题,因为它是音频流​​而不是静态文件。 There is also a link in this footer that when clicked will cause the entire static footer to disappear and a window to pop up with the audio stream. 此页脚中还有一个链接,当单击该链接时,将导致整个静态页脚消失,​​并弹出带有音频流的窗口。 Of course the problem is that if someone reloads the page or goes to another one the footer will reload. 当然,问题在于如果有人重新加载页面或转到另一个页面,则页脚将重新加载。 The pop up window is named through the same javascript that popped up the window. 弹出窗口是通过与弹出窗口相同的JavaScript来命名的。 This is also a problem if they leave the website and come back. 如果他们离开网站再回来,这也是一个问题。 I would like to figure out a way to run a check on the window loading to see if a window with a specific name is still open. 我想找出一种方法来检查加载的窗口,以查看是否仍打开具有特定名称的窗口。 So, if when I clicked on a button to get this pop up window I also named the window "Radio" then it would check to see if a window with the name radio exists. 因此,如果我单击一个按钮以获取此弹出窗口时,我也将该窗口命名为“ Radio”,则它将检查是否存在一个名为radio的窗口。 So something like: 所以像这样:

if window.name("Radio).exists then $(".radio").remove();

Something that runs after the document is ready. 文件准备好后便可以运行。 I just need a way to see if that named window is open then I can go from there. 我只需要一种方法来查看该命名窗口是否打开,然后就可以从那里去了。 Here's the jquery I have in the footer: 这是我在页脚中的jQuery:

$(".lnk1,#xx").click(function() {
  $("div").remove("#fixedBar")
});
$( document ).ready(function() {
$(".radio2").html('<audio src="http://www.live365.com/play/372874/" type="audio/mpeg" autoplay="autoplay" preload="auto" controls="controls"></audio>');}); 

Here's the HTML where I create the pop up link: 这是我创建弹出链接的HTML:

<div class="radioBar"><a class="lnk1" style="font-size:125%;text-align:center;" href="javascript://" onclick="window.open('/live365', 'live365',',width=400,height=550,resizable=yes,scrollbars=no,menubar=no'); return true;"><span style="padding:0 0 0 1%;">Click To Pop Out Player</span></a><div id="container1" class="radio2"></div></div>

Keep in mind that it's possible someone might leave the website so I don't think that using the window.opener is going to work here. 请记住,有人可能会离开网站,所以我不认为使用window.opener可以在这里工作。 I can use php, javascript, jquery, css, html, and if desperate, sledge hammer. 我可以使用php,javascript,jquery,css,html,如果不顾一切,可以使用大锤。 :) Thanks in advance for any ideas. :)预先感谢您的任何想法。 <--->EDIT<---> Here are some details I left out: I'm using straight HTML5 audio in wordpress and since mediaelement.js hates streaming audio and is basically crap in wordpress, I had to create a custom template and then float it statically in an iframe at the bottom of the website. <--->编辑<--->这是我遗漏的一些细节:我在wordpress中使用纯HTML5音频,并且因为mediaelement.js讨厌流音频并且基本上在wordpress中是废话,所以我不得不创建一个自定义模板然后将其静态浮动在网站底部的iframe中。 This added some complexities with other links on a page that might remove the frame. 这增加了页面上其他链接的复杂性,可能会删除框架。 So based on the answer left by Jack I put the following javascript/jquery code in my wordpress template. 因此,根据杰克留下的答案,我在我的wordpress模板中放置了以下javascript / jquery代码。

$( document ).ready(function() {
if (window.localStorage.radioOut === "1") {
  $("#fBar").remove();
}
$(".lnk2,#xx").click(function() {
  $("div").remove("#fbar");
      window.localStorage.radioOut = "1"
});
$(".radio2").html('<audio src="http://www.live365.com/play/372874/" type="audio/mpeg" autoplay="autoplay" preload="auto" controls="controls"></audio>');
}); 

in the html of the same template I added a class of lnk2 to the link on the page. 在同一模板的html中,我向页面上的链接添加了lnk2类。 In other parts of the site I added a class of lnk1 to all links that pop up the player on the entire site. 在该网站的其他部分,我向整个网站上弹出播放器的所有链接添加了lnk1类。 I added additional code to the footer of the site: 我在网站的页脚中添加了其他代码:

$(".lnk1").click(function() {
  $("div").remove(".fixedBar")
});
if (window.localStorage.radioOut === "1") {
  $(".fixedBar").remove();
}

So far this works perfectly as I have another template that handles the pop up audio player and it has it's own header. 到目前为止,这非常理想,因为我有另一个处理弹出音频播放器的模板,并且它具有自己的标题。 Since I deduced that the other aspect of the code is to change the value of local storage should that pop up window be closed and since I had two different pop up players that both used the same header, I added this code to the header file. 由于我推断出代码的另一方面是在关闭弹出窗口时更改本地存储的值,并且由于我有两个使用相同标头的不同弹出播放器,因此我将此代码添加到了标头文件中。

 window.addEventListener("beforeunload", function (event) {
  window.localStorage.radioOut = "0"
});

This is now live on the website and so far it appears to be working perfectly. 现在,它已经在网站上发布,并且到目前为止似乎运行良好。 If anyone is interested you can find it here: http://www.kgraradio.com KUDOS to Jack and everyone else who helped. 如果有人感兴趣,可以在这里找到: http: //www.kgraradio.com杰克(Kack)的KUDOS和其他提供帮助的人。

It sounds like you just want to track the state of the popup being open. 听起来您只想跟踪弹出窗口的打开状态。 I'd just use localStorage instead since I don't think you'll be able to do what you're talking about. 我只使用localStorage,因为我认为您将无法执行您所谈论的事情。

When the popup is opened do: 打开弹出窗口时,请执行以下操作:

window.localStorage.radioOut = "1"

In the popup itself: 在弹出窗口中:

window.addEventListener("beforeunload", function (event) {
  window.localStorage.radioOut = "0"
});

Then on your page just do: 然后在页面上执行以下操作:

if (window.localStorage.radioOut === "1") {
  $(".radio").remove();
}

You could have the click function that launches the pop out also set a cookie. 您可能具有启动弹出窗口的单击功能,还设置了一个cookie。 When the user leaves the site, then returns, you could have a check that looks for the cookie. 当用户离开站点然后返回时,您可以进行检查以查找cookie。 If the cookie exists, hide the footer bar, else, show it. 如果cookie存在,则隐藏页脚栏,否则显示它。

Since you are using jQuery, a cookie plugin could make it simpler https://cdnjs.com/libraries/jquery-cookie . 由于您使用的是jQuery,因此Cookie插件可以使其更简单https://cdnjs.com/libraries/jquery-cookie

Maybe something like: 也许像这样:

$(window).load(function() {
  if (!$.cookie('playerPop')) {
    $('.radio').show;
  }
});    

$(".lnk1,#xx").click(function() {
  $("div").hide("#fixedBar");
  $.cookie('playerPop', {
        path: '/',
        expires: 1
    });
});

You could use a technique like this to detect the closing of the popup: http://www.sitepoint.com/jquery-capture-close-popup-window/ . 您可以使用类似的技术来检测弹出窗口的关闭: http : //www.sitepoint.com/jquery-capture-close-popup-window/

Not only would closing the popup remove the cookie, but it would be cool if you "popped the player back into the page" by showing the footer bar again. 关闭弹出窗口不仅会删除Cookie,而且如果您再次显示页脚栏将播放器“弹出页面”,那将很酷。

since storage events fire on all tabs on the same domain (except on the tab setting the value), they can be used to poll for other tabs. 由于storage事件会在同一域中的所有选项卡上触发(设置值的选项卡除外),因此可以使用它们来轮询其他选项卡。

Listen for marco storage events on the popup tab, and trigger a polo event when they occur: 在弹出选项卡上侦听marco存储事件,并在事件发生时触发polo事件:

addEventListener("storage", function(e){
    if(e.key=='marco') localStorage.polo = e.newValue;
});

On the main page, trigger a new marco storage event and listen for a polo event: 在主页上,触发一个新的marco存储事件并收听polo事件:

addEventListener("storage", function(e){
    if(e.key=='polo' && e.newValue == localStorage.marco){
         $(".radio").remove();
    }
});
localStorage.marco=Math.random();

this works on all tabs, iframes, and popups (at once), so it's a bit more flexible than looking for only popups. 这项功能可一次在所有标签,iframe和弹出式窗口中使用,因此比仅查找弹出式窗口要灵活一些。

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

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