PDA

View Full Version : Getting truncated substring of a referrer


chimly
02-28-2005, 09:21 AM
I'm trying to modify something like:

function checkreferrer(){
if (document.referrer.substr(0,18)=='http://www.123.com/'){
playshow1();
}else{
playshow2();
}
}

... to something that will check the end of a string of unknown length for a specific file, such as 'index2.html'. The purpose of this is that I don't have a domain name yet, but I want to trigger an event like onload="checkreferrer()" ONLY when returning from a specific page deep within the window. (Likewise, returning from 'index3.html' may trigger a third show...)

One work-around might be to keep the main page in a hidden iframe and then call the function from the 'index2' page, but that may cause too much pre-loading in this case.

The important point is we don't know the length of the URL, but we do know the name of the html file at the end. So the desired "IF" test is more like:

if (document.referrer.substr(xxx,xxx)=='index2.html'){
...
}

Or something.

liorean
02-28-2005, 09:38 AM
This looks like a mess, but it should do it.if(/^http:\/\/[\/]+\/index2\.html/.test(document.referrer))
dothis;
else
dothat;

chimly
02-28-2005, 08:00 PM
WOW. Thanks!

chimly
02-28-2005, 11:37 PM
Trying another approach..... something LIKE:



function checkReferrer(){

var urlString=document.referrer;

if(urlString.indexOf("index2")){
playShow2();
}else{
playShow1();
}
}


....
<body onload="checkReferrer();">
....


But it's not quite working, although there are no errors reported.