Hi Guys,
I would be grateful if someone could take a look at this script & tell me where I’m going wrong.
Background: When a customer browses our site, they navigate from the item pages back to the various indices by using a ‘back to index’ button; this is a javascript:history script that works well. But, when they have arrived at the item page from a search engine, then this button takes them back out of the site. This is turning people away & playing havoc with our stats.
The idea of the script below is that a cookie is given on an index page (page 1) (this has been copied from elsewhere & seems to work ok).
When the customer presses the ‘back to index’ button on the item page (page 2) the script tries to retreive the cookie. If it does not exist, then they have reached the page from outside the site & they are redirected to the mainindex.html page.
If the cookie exists then the javascript:history will work ok. In this case they are allowed to trigger the javascript:history & return to the index page they came from.
Please help! ?
Cheers
Dave
Page 1 This gives out the cookie
<SCRIPT LANGUAGE=”JavaScript”>
<!–
function GetCookie(name) {
var arg=name+”=”;
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j)==arg)
return “here”;
i=document.cookie.indexOf(” “,i)+1;
if (i==0) break;
}
return null;
}
var visit=GetCookie(“blu001”);
if (visit==null){
var expire=new Date();
expire=new Date(expire.getTime()+7776000000);
document.cookie=”blu001=here; expires=”+expire;
}
// –>
</SCRIPT>
Page 2 This interogates for the cookie & redirects accordingly on the press of the button
<Head>
<SCRIPT LANGUAGE=”JavaScript”><!–
function GetCookie() {
if(GetCookie(“blu001”) == null)
{
document.location.href=”mainindex.html”;
}
else {
<document location.href=”history.go(-1)”;
}
}
–>
</script>
</Head>
<Body>
<FORM>
<INPUT TYPE=”button” VALUE=”BACK to Index” onClick=’GetCookie()’>
</FORM>
</Body>