Search code examples
javascriptdomain-name

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


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.


Solution

  • How about:

    window.location.hostname
    

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