Check if a tab is open on a mobile device?
I have a button on my web page that opens a new tab if my visitor appears on a mobile device.
code:
if (navigator.userAgent.match(/(iPhone|iPod|iPad|BlackBerry|Android|IEMobile)/)) {
window.open(the_URL);
}
How can I check if a visitor got an open tab from that link and returned the visitor to the open tab?
Value; I don't want to open a new tab every time a visitor clicks on this link.
Any ideas how to do this?
Update
I got this working in Safari (desktop)
var newWin;
if(newWin) {
try {
newWin.focus();
}catch(e) {
newWin = window.open(the_URL);
}
} else {
newWin = window.open(the_URL);
}
However, it does not switch to another tab on Safari iOS.
Update 2
Tried closing the window first and then reopening it. However .close
does not work
if(newWin) {
setTimeout(newWin.close, 1000);
try {
newWin.close();
newWin.focus();
}catch(e) {
newWin = window.open(the_URL);
}
} else {
newWin = window.open(the_URL);
}
source to share
Have you tried using the window name property?
From MDN:
strWindowName
The string name for the new window. The name can be used as the target of links and forms using the target attribute or element. The name must not contain spaces characters. Note that strWindowName does not indicate the title of the new window.
So change your code like below
if (navigator.userAgent.match(/(iPhone|iPod|iPad|BlackBerry|Android|IEMobile)/)) {
window.open(the_URL, 'newWindowName');
}
source to share