I am posting this just because I finally figured out how to do this and found very little simple information on how to make this work. Here is what the situation was…
I have a page that should only be displayed if the ‘disclaimer’ page has been viewed and the terms have been accepted. Once you click ‘accept’, you should be directed to the appropriate page, which we will call ‘agreedTerms.html’. The cookie is set to last for one year based on the ‘365’ you see in the code. Change that if you want to lessen the days.
Here is the code for each page:
DISCLAIMER.HTML ---------------------------------------------------
CODE FOR SETTING THE COOKIE ON THE DISCLAIMER PAGE (goes in head tag):
<script language = "JavaScript">
<!-- Begin hiding
function getCookieExpireDate(noDays){
var today = new Date()
var expr = new Date(today.getTime()+noDays*24*60*60*1000*365)
return expr.toGMTString()
}
function makeCookie(name, data, noDays){
var cookieStr = name + "="+ data
if (makeCookie.arguments.length > 2){
cookieStr += "; expires=" + getCookieExpireDate(noDays)
}
document.cookie = cookieStr
var hello="agreedterms.html";
window.location=hello;
}
function noway(){
var goodbye="index.html";
window.location=goodbye;
}
// End hiding -->
</script>
CODE FOR THE LINKS TO COMMIT THE ACTIONS on the DISCLAIMER PAGE (goes in the body tags):
<p><a href="agreedterms.html" onclick='makeCookie("disclaimer", "agreed", 1)'>AGREE</a></p>
<p><a href="index.html" onclick="noway()">DECLINE</a></p>
AGREEDTERMS.HTML ---------------------------------------------------
CODE TO SEE IF COOKIE IS SET AND REDIRECT (goes in head tags):
<script language = "JavaScript">
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie (cookieName) {
var arg = cookieName + "=";
var argLength = arg.length;
var cookieLength = document.cookie.length;
var i = 0;
while (i < cookieLength) {
var j = i + argLength;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal(j)
}
if (i == 0) {
break
}
}
return null;
}
if(getCookie('disclaimer') == null) {
location.href="disclaimer.html"
}
// End -->
</script>
Hope this helps someone!