gpt4 book ai didi

javascript - 检查选项卡是否已在移动设备上打开?

转载 作者:行者123 更新时间:2023-12-04 02:07:38 25 4
gpt4 key购买 nike

我的网页上有一个按钮,如果我的访客来自移动设备,它会打开一个新标签。

代码:

if (navigator.userAgent.match(/(iPhone|iPod|iPad|BlackBerry|Android|IEMobile)/)) {
window.open(the_URL);
}

我如何检查访问者是否已从该链接获得打开的选项卡并将访问者带回打开的选项卡?

意义;我不想每次访问者单击此链接时都打开一个新选项卡。

有什么想法可以实现吗?

更新

我在 Safari(桌面)中运行了它

var newWin;

if(newWin) {
try {
newWin.focus();
}catch(e) {
newWin = window.open(the_URL);
}
} else {
newWin = window.open(the_URL);
}

但是它没有切换到 iOS Safari 上的其他选项卡..

更新 2

试图先关闭窗口然后再重新打开它。但是 .close 似乎不起作用

    if(newWin) {
setTimeout(newWin.close, 1000);
try {
newWin.close();
newWin.focus();
}catch(e) {
newWin = window.open(the_URL);
}
} else {
newWin = window.open(the_URL);
}

最佳答案

您是否尝试过使用窗口名称属性?

来自 MDN:

strWindowName

A string name for the new window. The name can be used as the target of links and forms using the target attribute of an or element. The name should not contain any whitespace characters. Note that strWindowName does not specify the title of the new window.

所以修改你的代码如下

if (navigator.userAgent.match(/(iPhone|iPod|iPad|BlackBerry|Android|IEMobile)/)) {
window.open(the_URL, 'newWindowName');
}

关于javascript - 检查选项卡是否已在移动设备上打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com