UltraAlkaline
UltraAlkaline

Reputation: 61

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.

What I am trying to do is to disable that function with a cookie that has the expiration time of 20 minutes.

This is how far I've gotten with the 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.

And yes, the cookie is from w3schools.com.

Any help is appreciated.

Thanks.

Upvotes: 0

Views: 61

Answers (1)

Daniil Grankin
Daniil Grankin

Reputation: 3933

I guess you need something like this

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

Upvotes: 2

Related Questions