Welcome to WebmasterWorld Guest from 50.17.117.221

Forum Moderators: open

Message Too Old, No Replies

JavaScript to detect certain referrers

Check Part of Domain name against a list

     
9:55 am on Oct 9, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 17, 2001
posts: 130
votes: 0


I need this script to cycle thru an array and check if some word in the referrer's name is on a list - something like this?

<script language="JavaScript">
<!--
var myArray = [];
x[0]= 'domain1';
x[1]= 'domain2';
x[2]= 'domain3';
x[3]= 'domain4';
x[4]= 'domain5';
for(i=0;
i<5;
i++) {
if (document.referrer.indexOf(!myArray.indexOf') > -1);
}
document.write('Some Text goes here');
//-->
</script>

10:39 am on Oct 9, 2008 (gmt 0)

Senior Member

joined:Jan 27, 2003
posts:2534
votes: 0


You have the right idea - you need to loop through the array and check each entry against the referrer. Something like

var myArray = [];
myArray[0]= 'domain1';
myArray[1]= 'domain2';

var currentReferrer=document.referer;

var i;
for (i=0;i<myArray.length;i++)

// match() may be more effective than looking for an exact value
if (myArray[i] === currentReferrer) {

// the referrer matched one in the list
alert("matched referrer");

}

10:46 am on Oct 9, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


Try these changes, you may also need to wait for page onload.

<script language="JavaScript">
<!--
var myArray = [];
myArray[0]= 'domain1';
myArray[1]= 'domain2';
myArray[2]= 'domain3';
myArray[3]= 'domain4';
myArray[4]= 'domain5';
for(var i=0;
i<5;
i++) {
if (document.referrer.indexOf(myArray[i]) > -1)
{
document.write('Some Text goes here');
}}
//-->
</script>

10:53 am on Oct 9, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 17, 2001
posts: 130
votes: 0


Him Andy - thanks for the reply.

It doesn't fit what I need -
I need check "some word in the referrer's name is on a list"; not the whole referrer.

10:58 am on Oct 9, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 17, 2001
posts: 130
votes: 0


Hi DaveVK -

You're the Man - it worked instantly with your edits - many thanks.

Rhys