tags:

views:

33

answers:

2

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 document.referrer.hostname available in javascript. Any ideas how I can get this value?

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

+3  A: 

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

http://blog.stevenlevithan.com/archives/parseuri

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

Lou Franco
Thanks. The simple parsing works fine.
Keltex
+1  A: 

This would do:

document.referrer.split('/')[2];

Example.

Gert G