I am currently facing an issue where opening a new tab on click redirects the focus to the child window instead of staying in the current window. I would like the popup to open without blocking and allowing me to maintain focus on my current window.
The code snippet below demonstrates the functionality that is currently working:
/**
* For writing cookie
* @param name {String} name of the cookie
* @param value {*} value of the cookie
* @param days {Number} number of days
*/
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
/**
* Get cookie by a name
* @param name {String}
* @returns {*}
*/
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
/**
* Check if already redirected or not. If not, then redirect to targeted site.
*/
function redirectToTarget() {
var isAlreadyRedirected = getCookie('redirect');
if (null === isAlreadyRedirected) {
setCookie('redirect', 1, 30); // write for 1 month
window.open('https://www.google.com');
}
}
To call the JavaScript function from the body onclick event, use the following method:
<body onclick="redirectToTarget()">
Any assistance with this issue would be greatly appreciated.