40

Possible Duplicate:
Whats the difference between window.location.host and window.location.hostname

I'm looking at the window variable using the console and I notice that there are two different fields, one named host and the other hostname. After I've checked a few sites, there seems to be the same value in both. What is the difference between them? Which should be used for what purpose?

Community
  • 1
  • 1
  • If you're refering to `window.location`, I've always used host. I didn't even know there was `hostname`. Will be interesting to see what others say. –  Dec 02 '12 at 20:18
  • 2
    Have a look at https://developer.mozilla.org/en-US/docs/DOM/window.location#Properties. It's a list of properties with examples. – Felix Kling Dec 02 '12 at 20:18

2 Answers2

64

As specified by the definition:

  • hostname is the host name (without the port number or square brackets)
  • host is the host name and port number

So depending on your needs, you should use the one or the other. Most of the HTTP communication will go on default port 80, so you might omit it then. On the other hand, if you suspect that you need to take regard to non-standard port settings, you need to include that information in your source code as well.

2540625
  • 11,022
  • 8
  • 52
  • 58
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
21

Say we have this example:

example.org:1111

The hostname is the name - example.org

The host includes both the host name, and any port numbers associated - example.org:1111

dsgriffin
  • 66,495
  • 17
  • 137
  • 137