Keltex Keltex - 1 year ago 56
Javascript Question

How do I get the referrer's domain/host name using JavaScript?

I know I can get the host name of the current page, by simply doing:

var myhostname = location.hostname;

But how do I get the host name of the referrer? I can get the referrer by

var referrer = document.referrer;

but unfortunately there's no
available in JavaScript. How can I get this value?

An example of where this is useful is if somebody clicks a link on I want to be able to retrieve from the referrer (not the page and the query string).


By parsing it. referrer.split( '/' ); will get you close. Or take a look at this

If referrer is coming from a browser, it will be sane -- but just in case you want more robust parsing.