Checking the referrer
Asked Answered
P

6

36

I'm using this to check if someone came from Reddit, however it doesn't work.

var ref = document.referrer;
if(ref.match("/http://(www.)?reddit.com(/)?(.*)?/gi"){
    alert('You came from Reddit');
} else {
    alert('No you didn\'t');
}

Suggestions on the regular expression are most welcome too.

Patch answered 8/1, 2010 at 22:53 Comment(0)
P
109

Try this:

if (ref.match(/^https?:\/\/([^\/]+\.)?reddit\.com(\/|$)/i)) {
  alert("Came from reddit");
}

The regexp:

/^           # ensure start of string
 http        # match 'http'
 s?          # 's' if it exists is okay
 :\/\/       # match '://'
 ([^\/]+\.)? # match any non '/' chars followed by a '.' (if they exist)
 reddit\.com # match 'reddit.com'
 (\/|$)      # match '/' or the end of the string
/i           # match case-insenitive
Polecat answered 8/1, 2010 at 22:56 Comment(1)
+1 for the in-depth explanation of the regular expression. Very helpful.Clostridium
R
14

Close your if paren...

Rayfordrayle answered 8/1, 2010 at 22:56 Comment(1)
:) Everybody makes mistakes like that. Use something that'll give you errors (like Firebug), because staring at a silently failing JavaScript page is a PITA.Rayfordrayle
A
7

I've been using an alternative to RegEx by looking for the domain in the referrer

if (document.referrer.indexOf('reddit.com') >= 0) { alert('They came from Reddit.com'); }

EDIT: As thekingoftruth points out that doesn't work if reddit.com is included in an URL parameter so I've extended it a little. I've also added toLowerCase() as I spotted that in the RegExp above.

if (document.referrer.indexOf('?') > 0){
    if (document.referrer.substring(0,document.referrer.indexOf('?')).toLowerCase().indexOf('reddit.com') >= 0){
    alert('They came from Reddit');
    }
} else {
    if (document.referrer.toLowerCase().indexOf('reddit.com') > 0){
            alert('They came from Reddit');
    }
}
Arvy answered 13/6, 2014 at 17:10 Comment(3)
The only issue is if the referrer contains a sub-url within it as a query parameter, say.Lananna
Fair point thekingoftruth. I suppose I could do a check for a ? and look before that. I've also added a toLowerCase(). I'm just a little sad (if not surprised) that the one line solution isn't anymore :-)Arvy
I hear you. Javascript really makes it hard to have compact one-liners. :(Lananna
B
2

Try this:

ref.match(new RegExp("^http://(www\\.)?reddit\\.com/", "i"))

Or:

ref.match(/^http:\/\/(www\.)?reddit\.com\//i)
Blueing answered 8/1, 2010 at 22:56 Comment(0)
H
1

I would use this, wouldn't it be a lesser and simply way?

  var referral= document.refferer;
   If(referral.includes("www.reddit.com"){
  alert("you came from reddit");
   }
     else{
    alert("you didn't come from reddit");
       {
Herren answered 30/4, 2020 at 10:58 Comment(0)
C
-4

Use var ref = document.referer; // ONE R instead of TWO

Cloverleaf answered 15/3, 2014 at 18:18 Comment(1)
This matches exactly what I was looking for; the referer in the header.Honk

© 2022 - 2024 — McMap. All rights reserved.