Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  16] [ 2]  / answers: 1 / hits: 91461  / 14 Years ago, thu, august 5, 2010, 12:00:00

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


More From » javascript

 Answers
53

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.


[#96009] Tuesday, August 3, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
;