56

They both seem to give me the same thing (the domain name currently executing the script).

So what's the difference (if any) and where should each be used?

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224

1 Answers1

89

MDN Web Docs - window.location

host....: the host name and port number. [www.google.com]:80
hostname: the host name (without the port number or square brackets). www.google.com

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • In my firebug console I get the same though - `domain.com` and `domain.com`. No square brackets, no port number. Here is the code: `console.log(window.location.host); console.log(window.location.hostname);` – Thomas Clayson Jul 01 '11 at 14:20
  • @Thomas it could be that the port is explicit only if it's non-standard (= not 80) – Pekka Jul 01 '11 at 14:21
  • Fair enough. So, if I want the current domain name, its hostname. :) Many thanks - will accept as answer in 4 minutes! :p – Thomas Clayson Jul 01 '11 at 14:23
  • 1
    @Pekka In if you have example "http://localhost:80" which contains port 80, in that case "window.location.host" and "window.location.hostname" are same because 80 is default port. If you have "http://localhost:54198/" you will have following results: window.location.host = "localhost:54198" and window.location.hostname = "localhost". – kat1330 Nov 20 '15 at 21:24
  • So then the difference between `host` & `origin` is that `origin` also has `protocol` attached? – Cody Dec 21 '18 at 17:42