Can I track the referrer of a new window link?

ptrinh picture ptrinh · Jul 12, 2011 · Viewed 16.9k times · Source

Case:

  • site A contains a link that opens site B in new window.
  • The script in site B is trying to get the site A url.

My question: Is there any way to track the referrer of a new window which was opened using target="_blank" or Javascript window.open() function?

If yes then is there any way to completely hide the referrer?

Answer

Anthony picture Anthony · Jul 12, 2011

Referrer is in the HTTP header and so it will be available regardless of whether the window is blank or new. You can get the URL with:

console.log(document.referrer);

There are sites that use this to protect their sites from things like click-jacks, so it would be inappropriate to hide the referrer from the linked page. Browsers that allow for referrer spoofing are considered unsafe, and it tends to be patched when found.

Update

Maybe it's not as frowned upon as I thought. HTML5 has a property for <a> to not send the referrer in the HTTP headers: rel = "noreferrer".