javascript onclick留下

时间:2018-05-24 08:25:01

标签: javascript onclick

目前,我在点击我的身体时打开一个新选项卡,但它将焦点更改为子窗口。但我想通过留下窗口来做到这一点,以便弹出窗口不会阻塞,我可以把注意力集中在我当前的窗口上。

以下代码目前正在运作

Main.strings

从body onclick调用js函数,如下所示

 /**
* 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 for 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');
 }
}

任何类型的帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

在新标签页中打开您的网站,然后在当前标签中重新加载目标网址。以下JS代码段可以为您提供帮助。

function redirectToTarget() {
    var isAlreadyRedirected = getCookie('redirect');
    if (null === isAlreadyRedirected) {
        setCookie('redirect', 1, 30); // write for 1 month

        var current_loc = window.location.href;
        window.location.href = 'https://www.google.com';
        window.open(current_loc);
    }
}