Satch3000
Satch3000

Reputation: 49384

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.

Upvotes: 6

Views: 18716

Answers (3)

user319198
user319198

Reputation:

Using Javascript ReadCookie() Function

ReadCookie(), you realize immediately, is used to read a cookie.

You can read any cookies, provided they are being read on the same domain they were set at.

<script type="text/javascript" language="JavaScript">
var acookie = ReadCookie("cookiename");
if(acookie.length == 0)
 { 
  //redirect somewhere 
 }
</script>

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

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.

Upvotes: 8

Mala
Mala

Reputation: 14823

if( $.cookie('cookiename') == null ) { 
    alert("OH NOES U NO HAS COOKIE");
    window.location.replace('http://url');
}

Upvotes: 9

Related Questions