仅当窗口未打开时才打开Window.open [英] Window.open only if the window is not open

查看:73
本文介绍了仅当窗口未打开时才打开Window.open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个链接,可以打开一个播放非常长的音频文件的页面的新窗口。我当前的脚本可以正常打开页面,如果多次单击链接则不刷新。但是,当我移动到我的网站上的单独页面并再次单击此链接时,它会重新加载。我知道当父元素发生变化时,我将丢失变量,因此我需要打开窗口,覆盖现有内容。我试图找到解决方案。我宁愿不使用cookie来实现这一点,但如果需要,我会。

I have a link on my site that opens a new window to a page that plays a very long audio file. My current script works fine to open the page and not refresh if the link is clicked multiple times. However, when I have moved to a seperate page on my site and click this link again, it reloads. I am aware that when the parent element changes, I will lose my variable and thus I will need to open the window, overiding the existing content. I am trying to find a solution around that. I would prefer not to use a cookie to achieve this, but I will if required.

我的脚本如下:

function OpenWindow(){
    if(typeof(winRef) == 'undefined' || winRef.closed){
    //create new
    winRef = window.open('http://samplesite/page','winPop','sampleListOfOptions');
    } else {
    //give it focus (in case it got burried)
     winRef.focus();
    } 
}


推荐答案

你应首先调用 winRef = window.open(,winPopup)不带URL - 这将返回一个窗口(如果存在),无需重新加载。并且只有 winRef null 或空窗口,然后创建新窗口。

You should first to call winRef = window.open("", "winPopup") without URL - this will return a window, if it exists, without reloading. And only if winRef is null or empty window, then create new window.

这是我的测试代码:

var winRef;

function OpenWindow()
{
  if(typeof(winRef) == 'undefined' || winRef.closed)
  {
    //create new
    var url = 'http://someurl';
    winRef = window.open('', 'winPop', 'sampleListOfOptions');
    if(winRef == null || winRef.document.location.href != url)
    {
      winRef = window.open(url, 'winPop');
    }
  }
  else
  {
    //give it focus (in case it got burried)
    winRef.focus();
  } 
}

可行。

这篇关于仅当窗口未打开时才打开Window.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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