/    Sign up×
Community /Pin to ProfileBookmark

Newbie in Desperate need of help!

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>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@James_GatkaMar 23.2006 — mainindex.html:

[CODE]<html>
<head>
<script type="text/javascript">

var expDate = new Date();
expDate.setTime(expDate.getTime()+90*24*60*60*1000); // 90 days;

function setCookie(isName,isValue,dExpires){

document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}

function init(){

setCookie("blu001","1",expDate);
}

onload=init;

</script>
</head>
<body>

This is mainindex
<br><br>

<a href="item.html"> Item Page </a>

</body>
</html>[/CODE]


item.html:

[CODE]<html>
<head>
<script type="text/javascript">

function getCookie(isName){

var cookieStr = document.cookie;
var startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
var endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
var isData = cookieStr.substring(startSlice,endSlice)
var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function init(){

if (getCookie("blu001"))
{history.back()}
else {window.location.replace("mainindex.html")}
}

</script>
</head>
<body>

<input type="button" value="BACK to Index" onclick="init()">

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@heliosstudiosMar 23.2006 — I'm not trying to be rude in saying this, but can't you just make the button that goes back to the index a link to the mainindex.html page? What's the benefit in doing the history.back() function?
Copy linkTweet thisAlerts:
@OgwainauthorMar 23.2006 — Hi James,

Many thanks! I shall have this loaded for test as soon as I get home tonight.

The reason this is needed, is that all the pages on our site are hard coded, and as a result higher ranking. The item pages are individual, but can be called from up to seven index pages, hence the need to be able to back-track rather than simply follow a hard link.

Once again, many thanks!

Cheers

Dave
×

Success!

Help @Ogwain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...