user2645738
user2645738

Reputation: 202

document.referrer not working as expected in Firefox

I tried to use document.referrer. It's working as expected in Chrome, but it's not working as expected in Firefox.

if (document.referrer.indexOf('stringToCheck') > 0) {
    //code goes here    
}

What happens is,

  1. If I redirect from some page to this page, it gives the proper url (previous page), that's fine.
  2. If I directly type in the URL it gives me a blank string, that's also fine.
  3. But, if the page gets reloaded, then it gives me the same page URL in Chrome (thats my expected result), but in Firefox it still gives the previous page URL. In some cases it also gives a blank URL.

What would be the issue? Are there any recommendations or alternatives?

Firefox version: 37.0

Upvotes: 5

Views: 4304

Answers (1)

Etheryte
Etheryte

Reputation: 25310

Looking at the spec we see that

referrer of type DOMString, readonly
Returns the URI (IETF RFC 2396) of the page that linked to this page. The value is an empty string if the user navigated to the page directly (not through a link, but, for example, via a bookmark).

the behavior that you're working with (refreshing the page) isn't explicitly defined.
This means that it's a question of subjective interpretation, and neither browser is doing anything wrong per se, they simply interpret the implication differently:

  • Chrome assumes that reloading the page means you were linked to the page you're currently on by the very same page.
  • Firefox updates the referrer to a non-empty value only when you're actually linked to a new page.

Both interpretations have use cases when one or the other is better, but neither is wrong or right since the spec is vague.

Upvotes: 3

Related Questions