Redirect to new page if cookie doesn't exist or if the value of cookie is 'false'
Posted
by liquilife
on Stack Overflow
See other posts from Stack Overflow
or by liquilife
Published on 2010-05-28T20:18:48Z
Indexed on
2010/05/28
20:22 UTC
Read the original article
Hit count: 579
cookies
I have a DOB 'Are you over 18' script which when the form is filled out, validates if they are over 18 or not for a tobacco site I am working on. When this form is filled it stores a cookie 'ageCheck' with a value of either true or false. I'm having some issues with a bit of javascript which is stored on all the pages to determine if they can view the page or if they need to be redirected to the verification page. The javascript is supposed to set a div ID to 'display:block' if passed and if no cookie is found OR the cookie value is false is supposed to redirect them to the age verification page. However, it's just not working. I see the cookies are set correctly. Below is my code.. any help is appreciated.
<script>
document.body.onload = function() {
var ageCheck = getCookie('ageCheck');
if ('true' == ageCheck) {
document.getElementById('content').style.display = '';
} else {
document.location = '/ageCheck.html';
}
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
</script>
© Stack Overflow or respective owner