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

问题描述 投票:1回答:2

是否可以仅在尚未打开的情况下打开新选项卡。

的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)保持打开状态,但它不活动。

javascript jquery
2个回答
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的更多信息。


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. */
  };
} 
© www.soinside.com 2019 - 2024. All rights reserved.