是否可以仅在尚未打开的情况下打开新选项卡。
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 的更多信息。 .
关于javascript - 仅当新选项卡尚未打开时才打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53355304/