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.)
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:
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:
If your form tag already has an onsubmit attribute, the
attribute can be put into the input tag with the submit
button instead. Example:
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:
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.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]