Get The Current Domain Name With Javascript (Not the path, etc.)

Freesnöw picture Freesnöw · Jul 9, 2012 · Viewed 324.4k times · Source

I plan on buying two domain names for the same site. Depending on which domain is used I plan on providing slightly different data on the page. Is there a way for me to detect the actual domain name that the page is loading from so that I know what to change my content to?

I've looked around for stuff like this but most of it doesn't work the way I want it to.

For instance when using

document.write(document.location)

on JSFiddle it returns

http://fiddle.jshell.net/_display/

i.e. the actual path or whatever that is.

Answer

Gareth picture Gareth · Jul 9, 2012

How about:

window.location.hostname

The location object actually has a number of attributes referring to different parts of the URL