Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If cookie does not exist alert and redirect

I need to check if a cookie exists when the user lands on a page and if the cookie does not exist I need to popup an alert and then redirect to another page.

like image 232
Satch3000 Avatar asked Feb 04 '12 22:02

Satch3000


2 Answers

if( $.cookie('cookiename') == null ) { 
    alert("OH NOES U NO HAS COOKIE");
    window.location.replace('http://url');
}
like image 102
Mala Avatar answered Sep 18 '22 12:09

Mala


if( document.cookie.indexOf("cookiename=") < 0) {
    alert("Cookie not found, redirecting you.");
    location.href = "newpage.html";
}

Be careful not to use a cookie name that may be the end of another cookie name. If this is likely, you'll need to do a full cookie read, or use PHP instead.

like image 41
Niet the Dark Absol Avatar answered Sep 21 '22 12:09

Niet the Dark Absol