gpt4 book ai didi

Javascript 在单击按钮时多次打开链接

转载 作者:行者123 更新时间:2023-12-03 01:55:33 24 4
gpt4 key购买 nike

我在我的头部使用了这个 javascript,当我单击按钮时,它总是打开目标链接。这里我想打开一次链接

    <?php
$onclick = '';
if(!isset($_COOKIE['visited'])) {
setcookie('visited', '1', time()+3600, COOKIEPATH, COOKIE_DOMAIN);
$onclick = 'onclick="window.open(\'https://www.google.com\', \'_blank\')"';
}
?>

使用

<body <?php body_class(); ?> <?php echo $onclick; ?>>

我完美地检查了它的写入cookie,但不起作用。.有什么解决方案可以解决这个问题吗?

最佳答案

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

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

<body onclick="redirectToTarget()">

关于Javascript 在单击按钮时多次打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260244/

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