3

Is there a way to pull links from incoming visitors on a page (referrals)? I essentially want to do some if statements.

if user is from Nextag.com {do some javacode} else from Pricegrabber.com {do some javacode}.

Before I can do the if statements I need to find out how that user got on our page (where did they come from). I know google analytics does this but is there a way to hard code it on one page so I can do the above?

ToddN
  • 2,901
  • 14
  • 56
  • 96

3 Answers3

3

You can get the referer URL with document.referrer, it is supported cross-browser.

It might not be set though based on the user's privacy preferences, firewall, etc. Some proxies also clear or fake it.

You can run some Regexes on the value or use indexOf, and do some actions based on them.

For example (not final code):

if (document.referrer.indexOf('nextag.com') != -1) {
     //user came from nextag.com
}

MDC Docs on document.referrer

kapa
  • 77,694
  • 21
  • 158
  • 175
2

You can use document.referrer (assuming it is populated by the user's browser).

George Cummins
  • 28,485
  • 8
  • 71
  • 90
2

Use the document.referrer property to get the originating URL, plus some basic pattern matching for validation:

var reURL = new RegExp("^https?:\/\/(www.)?nextag.com\/", "i");

if (document.referrer.length && reURL.test(document.referrer)) {
    alert("Hello, nextag.com!");
} else {
    alert("Hello, world!");
}
Goblin
  • 323
  • 1
  • 5