简体   繁体   English

使用Cookie禁用JavaScript函数

[英]Disabling a JavaScript function with a Cookie

I am working on a mobile part of my site and I would like to disable a welcome message which is produced by a JavaScript function when the page loads. 我正在网站的移动部分上工作,我想禁用在页面加载时由JavaScript函数产生的欢迎消息。

What I am trying to do is to disable that function with a cookie that has the expiration time of 20 minutes. 我想做的是使用有效期为20分钟的cookie禁用该功能。

This is how far I've gotten with the cookie: 这是我对Cookie的了解程度:

// c_name = cookie name
// c_value = cookie value
// ex_min = expiration in minutes
function setCookie(c_name, c_value, ex_min) {
var d = new Date();
d.setTime(d.getTime() + (ex_min*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = c_name + "=" + c_value + "; " + expires;
}
// Cookie that lasts for 20 minutes.
setCookie("last-visited", "1", 20);

What I can't figure out is how to disable that function from within the cookie. 我不知道的是如何从cookie中禁用该功能。

And yes, the cookie is from w3schools.com . 是的,cookie来自w3schools.com

Any help is appreciated. 任何帮助表示赞赏。

Thanks. 谢谢。

I guess you need something like this 我想你需要这样的东西

var lastVisitedCookieName = 'last-visited';
if (!document.cookie.contains(lastVisitedCookieName))
{
    alert('Welcome');
    setCookie(lastVisitedCookieName, "1", 20);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM