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).
The getHost() method of URL class returns the hostname of the URL. This method will return the IPv6 address enclosed in square brackets ('['and']').
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.
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.
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.
This would do:
document.referrer.split('/')[2];
Example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With