Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the referrer

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.

like image 847
Ben Shelock Avatar asked Jan 08 '10 22:01

Ben Shelock


People also ask

How do I check my referrer?

To check the Referer in action go to Inspect Element -> Network check the request header for Referer like below. Referer header is highlighted. Supported Browsers: The browsers are compatible with HTTP header Referer are listed below: Google Chrome.

What is your referrer?

Your referrer is the page you're coming from. You can create a link to this page and click on it in order to check what URL If-So detects as the referral source. If you will not see your referrer above, that means neither If-So nor other services can track the referral source.

What is referrer example?

In more simple terms, the referer is the URL from which came a request received by a server. A good example is if you click a link on the page site.com/page to go to another-site.com/link, the HTTP Referer received by another-site.com/link will have the value site.com/page.

How do I check my referrer in Chrome?

To try out the change in Chrome, enable the flag at chrome://flags/#reduced-referrer-granularity . You can also check out this demo to see the change in action. Beyond the referrer policy, the way browsers deal with referrers might change—so keep an eye on it.


1 Answers

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 
like image 199
gnarf Avatar answered Sep 20 '22 12:09

gnarf