Forum Moderators: open

Message Too Old, No Replies

checking if referrer contains a specific text (url)

(for website statistics javascript)

         

Darkelve

8:06 am on Nov 20, 2007 (gmt 0)

10+ Year Member



Our web stats vendor refuses to help us with the following (do-it-yourself attitude... sigh), so I'm seeing if I can do it myself.

I need to do the following inside of a small piece of javascript:

1. Check if page url contains the text "http://www.example.com/page-to-exclude" (other formats of the URL like http://www.example.com/page-to-exclude?para=1&para=2 also exist)

1a If no: use the following code:
- code 1
1b If yes:
- do nothing

Is it possible to check through javascript if a url contains a specific piece of text (part of a string) like in the scenario above?

Many thanks!

Trace

2:51 pm on Nov 20, 2007 (gmt 0)

10+ Year Member



Try searching for "indexOf". What you'll want is something that looks like this;

if(document.referrer.indexOf("/page-to-exclude") > 0){
...do something...
}else{
...do something else...
}

Once you get that figured out you'll probably want to build an array of things to check against.