It's a Cookie! Get Outta Here!
When a page has performed it's purpose, a cookie can be set to redirect the browser to a different page on subsequent visits.
Once visitors click the link, use the form, see the page, or whatever, once they've done what you want them to do, they don't have to see the page again.
Limitations
Cookies are fallible. Browsers must accept cookies or none can be set. And the JavaScript code presented here requires browsers to be JavaScript-enabled.
Most browsers allow cookies and have JavaScript turned on.
If a browser is either cookies- or JavaScript-disabled, no cookie will be set and the browser will not be redirected on subsequent visits.
The First Thing To Do
The first thing that needs to be done is copy the JavaScript and paste it into the head area of your web page source code. (Alternatively, the JavaScript can be put into an external file that is included in the web page.)
<script type="text/javascript" language="JavaScript"> <!-- Copyright 2006 Bontrager Connection, LLC // Three items can be customized // (values between quotation marks): // // 1. // What URL shall the browser be redirected // to if a cookie was previously set? var RedirectURL = "https://www.willmaster.com"; // 2. // How many days shall the cookie live in // the visitor's browser (0 means remove // cookie whenever browser closes)? var DaysToLive = "365"; // 3. // What name shall the cookie have (any // sequence of alphabetical characters // is okay, so long as the name doesn't // conflict with any other cookies that // this web page might be setting.)? var CookieName = "HasVisited"; // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // No other customizations required. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // function Action() { location.href = RedirectURL; } DaysToLive = parseInt(DaysToLive); var Value = 'bypass page next time'; function GetCookie() { var cookiecontent = ''; if(document.cookie.length > 0) { var cookiename = CookieName + '='; var cookiebegin = document.cookie.indexOf(cookiename); var cookieend = 0; if(cookiebegin > -1) { cookiebegin += cookiename.length; cookieend = document.cookie.indexOf(";",cookiebegin); if(cookieend < cookiebegin) { cookieend = document.cookie.length; } cookiecontent = document.cookie.substring(cookiebegin,cookieend); } } if(cookiecontent.length > 0) { return true; } return false; } function SetCookie() { var exp = ''; if(DaysToLive > 0) { var now = new Date(); then = now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000); now.setTime(then); exp = '; expires=' + now.toGMTString(); } document.cookie = CookieName + '=' + Value + exp; return true; } if(GetCookie() == true) { Action(); } // --> </script>
The JavaScript has three places to customize:
-
The URL for the browser to be redirected to when a cookie is found.
-
The number of days the cookie shall stay in the browser.
-
The cookie name.
Once the JavaScript is in place, it will automatically redirect browsers with the cookie to the specified URL.
Now, all that's left to do is cause the cookie to be set when a certain action takes place.
Setting a Cookie When a Link Is Clicked
Let's suppose the action you want them to take is to click on a certain link. When the link is clicked, a cookie is set that will redirect subsequent visits to a different web page.
Putting an onclick attribute into the link anchor tag causes the cookie setting code to run:
<a href="/" onclick="SetCookie()"> Click for good stuff. </a>
After the link is clicked, subsequent visits to the page cause the browser to be redirected.
Setting a Cookie When a Form Is Used
Let's suppose you have a subscription form on your front page. The page is all about why the visitor should subscribe. Once they use the subscribe form, the page has done its job. Thereafter, they are redirected to an alternate front page.
To implement, put the
attribute into the form tag. It would look something like this:
<form action="http://example.com/cgi-bin/script.cgi" method="POST" onsubmit="SetCookie()" />
If your form tag already has an onsubmit attribute, the
attribute can be put into the input tag with the submit button instead. Example:
<input type="submit" value="Click Me!" onclick="return SetCookie()" />
The cookie is set when they use the subscription form, and the browser is then redirected to the alternate front page on subsequent visits.
Setting a Cookie When the Page Loads
Let's suppose you have a web page that should be seen only once. Whether or not they do what you want them to do, subsequent visits should be redirected to a different page.
In that case, let the "page load" action cause the cookie to be set. Put this JavaScript somewhere into the body area of your source code:
<script type="text/javascript" language="JavaScript"><!-- SetCookie(); //--></script>
Now, the cookie is set when the page loads and subsequent visits will be redirected.
Power User Customization
If you want to have something other than (or in addition to) a redirect take place when a browser with the cookie arrives, modify the function Action() in the JavaScript according to your needs. The function Action() is right below the "No other customizations required" mark in the JavaScript.
If code is to be run in addition to the redirect, the redirect code should be the last line of the function.
Simplicity
First, customize the JavaScript and paste it into your web page.
Next, cause SetCookie() to run when the trigger action takes place.
That's a lot of functionality for very little work.
Will Bontrager