Reputation: 49384
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
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
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
Reputation: 14823
if( $.cookie('cookiename') == null ) {
alert("OH NOES U NO HAS COOKIE");
window.location.replace('http://url');
}
Upvotes: 9