Welcome to WebmasterWorld Guest from 54.167.58.159

Forum Moderators: open

Message Too Old, No Replies

JavaScript to detect certain referrers

Check Part of Domain name against a list

     

Rhys

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

10+ Year Member



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>

Receptional Andy

10:39 am on Oct 9, 2008 (gmt 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");

}

daveVk

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

WebmasterWorld Senior Member 5+ Year Member



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>

Rhys

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

10+ Year Member



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.

Rhys

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

10+ Year Member



Hi DaveVK -

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

Rhys