Javascript 仅当新选项卡尚未打开时才打开它

Javascript 仅当新选项卡尚未打开时才打开它,javascript,jquery,Javascript,Jquery,仅当新选项卡尚未打开时,才可以打开它 index.php $('.title').on('click', function(){ if tab `images.php` is already open -> go to that tab else {window.open('images.php', '_blank') // open a new tab }); 因此,如果新选项卡已经存在,我不想打开它 在这两种情况下,当前选项卡(index.php)保持打开状态,但不

仅当新选项卡尚未打开时,才可以打开它

index.php

$('.title').on('click', function(){
    if tab `images.php` is already open -> go to that tab
    else {window.open('images.php', '_blank')  // open a new tab
});
因此,如果新选项卡已经存在,我不想打开它


在这两种情况下,当前选项卡(
index.php
)保持打开状态,但不处于活动状态。

只需为窗口指定一个名称,并确保该名称不以下划线开头,否则其行为将类似于html的
\u target
属性:

$('.title').on('click', function(){
  window.open('images.php', 'myWindow')
  // it will open the same window on following clicks
});
如果不想重新加载页面,请执行以下操作:

var myWindow = null;
$('.title').on('click', function(){
  if(myWindow == null || myWindow.closed)
    myWindow = window.open('images.php', 'myWindow')
  else
    myWindow.focus()
});
您可以在从“最佳实践”部分复制的。

中阅读有关window.open的更多信息


相关问题可能会有帮助(如果不是重复的话):这是否也会导致打开的页面重新加载?我如何设置窗口名称,请?@TimSch Yes。第二个参数是窗口名,如示例所示。@bhojendraurauniyar,抱歉,我不明白,我首先需要检查窗口是否打开,所以我需要在
窗口之前设置它的名称。open…
@bhojendrauniyar,未选中,但似乎可以工作。谢谢
var windowObjectReference = null; // global variable

function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowObjectReference = window.open("http://www.spreadfirefox.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}