Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:检查重复打开的窗口_Javascript_Windows - Fatal编程技术网

Javascript:检查重复打开的窗口

Javascript:检查重复打开的窗口,javascript,windows,Javascript,Windows,是否可以检查同一窗口是否已打开 例如,我通过javascript打开了一个窗口 我可以通过javascript检查是否在另一个页面上打开了它吗 如果页面已经打开,只想将焦点放在页面上,以避免重复窗口 谢谢;) 查看window.open()方法。必须将窗口的名称指定为第二个参数。如果已存在具有该名称的窗口,则新URL将在已存在的窗口中打开,请参阅 如果您真的想检查,如果窗口是由您自己的脚本打开的,那么您必须在全局变量等中保留对打开窗口的引用,并使用 var myOpenedWindow = my

是否可以检查同一窗口是否已打开

例如,我通过javascript打开了一个窗口

我可以通过javascript检查是否在另一个页面上打开了它吗

如果页面已经打开,只想将焦点放在页面上,以避免重复窗口

谢谢;)

查看
window.open()
方法。必须将窗口的名称指定为第二个参数。如果已存在具有该名称的窗口,则新URL将在已存在的窗口中打开,请参阅

如果您真的想检查,如果窗口是由您自己的脚本打开的,那么您必须在全局变量等中保留对打开窗口的引用,并使用

var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
您还可以将此行为封装在方法中:

var myOpenWindow = function(URL) {
    var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
    myOpenedWindow.location.href= URL;
    myOpenedWindow.focus();
}
并使用
myOpenWindow()调用该函数http://www.example.com/');

如果您有父-子窗口,那么这里有一个解决方案,允许您检查是否从启动它的父窗口打开了子窗口。这将带来一场灾难 聚焦到子窗口而不重新加载其数据:

 <script type="text/javascript">
    var popWin;
    function popPage(url)
    {
       if (popWin &! popWin.closed && popWin.focus){
           popWin.focus();
       } else {
          popWin = window.open(url,'','width=800,height=600');
      }
    }
</script>

    <a href="http://www.xzy.com"
onclick="popPage(this.href);return false;">link</a>

var popWin;
函数弹出(url)
{
if(popWin&!popWin.closed&&popWin.focus){
popWin.focus();
}否则{
popWin=window.open(url,,'width=800,height=600');
}
}
还有一件事::---如果用户刷新父窗口,它可能会丢失所有数据 对可能已打开的任何子窗口的引用


希望这有帮助,并让我知道输出。

如果您想从链接打开url,这将有帮助

 var Win=null;
function newTab(url){
//var Win; // this will hold our opened window

// first check to see if the window already exists
if (Win != null) {
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!Win.closed) {
  Win.close();
//   return winObj;
}

 // otherwise fall through to the code below to re-open the window
 } 

 // if we get here, then the window hasn't been created yet, or it
 // was closed by the user.
 Win = window.open(url);

 return Win; 

 }
 newTab('index.html');

感谢mate提供此解决方案;)在较新的浏览器中,还可以使用来检测重复的选项卡。