JavaScripts :: Cookies :: Redirection Cookie
This small javascript allows you to set a cookie on your visitors computer. If the cookie still exists the next time they visit, they are redirected to the page of your choice. Ideal for redirecting visitors who have already seen you (long) flash intro or your popup windows. Tested on Mac and PC in Netscape, Opera and IE. Simply copy the following JavaScript to your document. There are two variables you can set. These are go_to, which is the URL your visitor should be redirected to, and num_days which is the number of days the cookie should remain on your visitor's computer. Once the cookie has expired, the process begins again. That is, the next time the visitor hits the page, the cookie is set and waits for the visitor's next hit.
The Script
<script>
<!--
/* Copyright http://www.perlscriptsjavascripts.com
Free and commercial Perl and JavaScripts */
// page to go to if cookie exists
go_to = "http://www.perlscriptsjavascripts.com";
// number of days cookie lives for
num_days = 60;
function ged(noDays){
var today = new Date();
var expr = new Date(today.getTime() + noDays*24*60*60*1000);
return expr.toGMTString();
}
function readCookie(cookieName){
var start = document.cookie.indexOf(cookieName);
if (start == -1){
document.cookie = "seenit=yes; expires=" + ged(num_days);
} else {
window.location = go_to;
}
}
readCookie("seenit");
// -->
</script>
Changing days to hours
If you would like the cookie to expire immediately or in a matter of hours or minutes, you can make the following modifications :
To have the cookie expire immediately, set the num_days variable to a negative number. Eg.
num_days = -1;
To have the cookie expire in a matter of hours, remove the *24 from the ged (Get Expiration Date) function. The num_days = 5; variable will now be the number of hours. For your information, the rest of the figures mean, times 60 seconds, times 60 minutes, times 1000 milliseconds.
|