是否可以仅在尚未打开的情况下打开新选项卡。
的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的_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的更多信息。
复制自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. */
};
}