Quite a readers enjoyed last week's Surfer Redirection Using JavaScript article, and a few of you wanted to know more about using JavaScript to manage your site's traffic. Here are a few more of my favorite redirection scripts, and some suggestions on how to use them...
Referrer Redirect
Being able to redirect surfers based upon the page they came from can provide you with many benefits, including the ability to target advertising and entry pages, as well as language settings and other user-specific enhancements. You can also use this script for a rudimentary security gate, ensuring that visitors to your member's area came through your AVS script page.
Place the following lines inside of your <HEAD></HEAD> tag. You will need to insert the page destination you wish to send the surfer where it says REDIRECTION PAGE as well as your BASE URL; the page that must refer the surfer:
<SCRIPT language="JavaScript">
<!-- Referrer Redirect
function check(){
referingDoc = document.hide.ref.value
if (referingDoc != ("")){
if (referingDoc.indexOf("BASE URL") == -1){;
window.location = "REDIRECTION PAGE"}
}
}
// Referrer Redirect -->
</SCRIPT>
Now put the following lines inside of your <BODY> code:
<BODY onLoad="check()">
Finally, place this code after your <BODY onLoad="check()"> tag:
<FORM name=hide><INPUT type=hidden name=ref></FORM>
<SCRIPT>document.hide.ref.value = document.referrer; </SCRIPT>
New User Redirect
Using cookies to determine if the user has been to your page before, this script redirects first time visitors to a new page. This is really handy for setting up a special greeting or help page.
Put these lines inside of your <HEAD></HEAD> tag, modifying the REDIRECT URL and COOKIE NAME (choose something "unique") to the appropriate values:
<SCRIPT language="JavaScript">
<!-- New User Redirect
var caution = false
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}
function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}
var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000)
var visits = getCookie("COOKIE NAME")
if (!visits)
location = "REDIRECT URL"
setCookie("COOKIE NAME", visits, now)
// New User Redirect -->
</SCRIPT>
Time Based Redirect
This simple JavaScript alternative to cookie-based solutions will send visitors to a specified URL after a set period of time. This is great for high bandwidth Web pages that you want to limit free access to. Perhaps you could even use it to set up a "limited time free trial" to a webcam or other video feed, sending surfers to the join form for more. The possibilities are endless...
Put these lines inside of your <HEAD></HEAD> tag:
<SCRIPT language="JavaScript">
<!-- Time Based Redirect
function startTimer(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
closeTime=hours*3600+mins*60+secs;
closeTime+=10;
Timer();
}
function Timer(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
curTime=hours*3600+mins*60+secs
if(curTime>=closeTime)
location = "YOUR URL"
else
timeLeft();
}
function timeLeft(){
count = closeTime-curTime;
window.setTimeout("Timer()",1000)
}
// Time Based Redirect -->
</SCRIPT>
Now put the following lines inside of your <BODY> code:
<BODY onLoad="startTimer()">
Finally, change closeTime+=10; to reflect the amount of seconds to wait before page redirect, and change location = "YOUR URL" to the URL you wish to send the surfer.
I hope that you find this collection of surfer redirection tools useful. They can open up new possibilities for your traffic management planning, and help you to increase your profitability. Don't be afraid to experiment, either. Combine bits and pieces of several scripts together and develop a solution unique to YOUR needs. Good luck!