Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 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).

like image 396
Keltex Avatar asked Aug 05 '10 23:08

Keltex


People also ask

How do I find the hostname of a URL?

The getHost() method of URL class returns the hostname of the URL. This method will return the IPv6 address enclosed in square brackets ('['and']').

How do I get the current domain in HTML?

window.location.href returns the href (URL) of the current page. window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page.

How do I get a domain name in react?

To access the domain name from an above URL, we can use the window. location object that contains a hostname property which is holding the domain name. Similarly, we can also use the document. domain property to access it.

What is domain in JavaScript?

Definition and UsageThe domain property returns the domain name of the server (the document was loaded from). The domain property returns null if the document was created in memory.


1 Answers

This would do:

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

Example.

like image 150
Gert Grenander Avatar answered Sep 28 '22 03:09

Gert Grenander