仅在尚未打开新选项卡时打开

时间:2018-11-17 20:31:52

标签: 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)均保持打开状态,但未处于活动状态。

2 个答案:

答案 0 :(得分:0)

只需为窗口命名,并确保其名称不要以下划线开头,否则其行为类似于html的_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()
});

您可以在docs中阅读有关window.open的更多信息。

答案 1 :(得分:0)

MDN window.open() docs“最佳做法”部分复制

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. */
  };
}