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

坚强是说给别人听的谎言 提交于 2019-12-09 07:38:06

问题


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

I think an explanation with an example would be helpful.


回答1:


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.



来源:https://stackoverflow.com/questions/4226111/javascript-difference-between-location-hostname-and-document-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!