69

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. How can I 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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Keltex
  • 26,220
  • 11
  • 79
  • 111

7 Answers7

58

This would do:

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

Example.

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
  • splitting on the / did not work for me. i had to split on the comma for some reason. it works, but not sure if this might be because of changes since 2010. – Josh Brown Jun 17 '14 at 16:41
  • To remove port you can use `document.referrer.split('/')[2].split(':')[0];` – Stephane L Jul 28 '18 at 13:58
42
function parseURL(url) {
    var a=document.createElement('a');
    a.href=url;
    return a.hostname;
}

This is a relatively old question, nevertheless this may help any followers.

adnan korkmaz
  • 489
  • 4
  • 3
30

By parsing it. document.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.

Community
  • 1
  • 1
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 3
    If you don't want to re-invent the parsing wheel: with `var a = document.createElement('a')` create an anchor element, then set `a.href = document.referrer` and then simply read out `a.hostname.` Or, even simpler (if you are using NPM), use [ulocation](https://npmjs.com/package/ulocation): `npm install -S ulocation`, `var Location = require('ulocation')` and use `Location(document.referrer).hostname`. *Disclaimer: I am the author of that package* – Stijn de Witt Jan 25 '17 at 21:20
16

You can use var referrer = new URL(document.referrer).hostname.

See https://developer.mozilla.org/en-US/docs/Web/API/URL.URL.

chipit24
  • 6,509
  • 7
  • 47
  • 67
  • 7
    Very clean but be aware it's not currently supported in IE https://developer.mozilla.org/en-US/docs/Web/API/URL.URL#Browser_compatibility – Robb Dec 03 '14 at 09:54
1

You can use regexp to extract this data.

string.match(/^http([s]?)://([a-zA-Z0-9-_\.]+)(:[0-9]+)?/);

My Stack Overfloweth
  • 4,729
  • 4
  • 25
  • 42
baquiax
  • 148
  • 13
0

Hi use this function to get domain name.

function getDomain(url) {
    if (url) {
        var match = /(?:https?:\/\/)?(?:\w+:\/)?[^:?#\/\s]*?([^.\s]+\.(?:[a-z]{2,}|co\.uk|org\.uk|ac\.uk|org\.au|com\.au))(?:[:?#\/]|$)/gi
                .exec(url);
        return match ? match[1] : null;
    } else
        return null;
}
Java4you
  • 576
  • 2
  • 10
  • 20
-1

It includes the protocol, but document.origin will work. It works via the Origin header, which has no path information included with it.

elkelk
  • 1,692
  • 2
  • 13
  • 20