Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Difference between location.hostname and document.domain?

What is the difference between using location.hostname and document.domain?

I think an explanation with an example would be helpful.

like image 953
Andreas Avatar asked Nov 19 '10 14:11

Andreas


People also ask

What is location hostname?

The location. hostname property returns the host (IP adress or domain) of a URL. The location. hostname property can also be set, to navigate to the same URL with a new hostname.

What does location hostname return?

The window.location.hostname property returns the name of the internet host (of the current page).

What is host in JS?

host property returns the host (IP adress or domain) and port of a URL. The location. host property can also be set, to navigate to the same URL with a new host and port.


1 Answers

It seems that document.domain is a read only property, except in Mozilla, which lets you change the value of the domain that is used for the same origin policy of (for example) AJAX requests without actually updating the page.

The restrictions on this are the same rules of the Same Origin Policy.

At least this is my understanding of the MDC docs for document.domain.

From the docs:

Gets/sets the domain portion of the origin of the current document, as used by the same origin policy.

...

In the DOM HTML specification, this property is listed as being read-only. However, Mozilla will let you set it to a superdomain of the current value, constrained by its base domain. For example, on developer.mozilla.org it is possible to set it to "mozilla.org" but not "mozilla.com" or "org".

Try updating document.domain and window.location.hostname to a new value in the console, and see the difference.

like image 92
user113716 Avatar answered Sep 22 '22 01:09

user113716