关于什么是location

API=application programming interface,location也是API的接口之一。

Location接口表示其关联的对象所展示的页面的地址等信息,对该对象的修改会反映到关联的对象上。Document 和 Window 接口都有一个关联的Location,可以分别用Document.locationWindow.location来访问它们对应的Location。

属性EDIT

Location没有继承任何属性,但实现了URLUtils的属性。

  • URLUtils.href

  • 包含整个URL的一个DOMString

  • URLUtils.protocol

  • 包含URL对应协议的一个DOMString,最后有一个":"。

  • URLUtils.host

  • 包含了域名的一个DOMString,可能在该串最后带有一个":"并跟上URL的端口号。

  • URLUtils.hostname

  • 包含URL域名的一个DOMString

  • URLUtils.port

  • 包含端口号的一个DOMString

  • URLUtils.pathname

  • 包含URL中路径部分的一个DOMString,开头有一个“/"。

  • URLUtils.search

  •  包含URL参数的一个DOMString,开头有一个“?”

  • URLUtils.hash

  • 包含块标识符的DOMString,开头有一个“#”。

  • URLUtils.username

  • 包含URL中域名前的用户名的一个DOMString

  • URLUtils.password

  • 包含URL域名前的密码的一个 DOMString

  • URLUtils.origin 只读

  • 包含页面来源的域名的标准形式DOMString

方法EDIT

Location没有继承任何方法,但实现了来自URLUtils的方法。

  • Location.assign()

  • 加载给定URL的内容资源到这个Location对象所关联的对象上。

  • Location.reload()

  • 重新加载来自当前 URL的资源。他有一个特殊的可选参数,类型为 Boolean,该参数为true时会导致该方法引发的刷新一定会从服务器上加载数据。如果是 false或没有制定这个参数,浏览器可能从缓存当中加载页面。

  • Location.replace()

  • 用给定的URL替换掉当前的资源。与 assign() 方法不同的是用 replace()替换的新页面不会被保存在会话的历史 History中,这意味着用户将不能用后退按钮转到该页面。

  • URLUtils.toString()

  • 返回一个DOMString,包含整个URL。 它和读取URLUtils.href的效果相同。但是用它是不能够修改Location的值的。

例子EDIT

// Create anchor element and use href property for the purpose of this example// A more correct alternative is to browse to the URL and use document.location or window.locationvar url = document.createElement('a');url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container';console.log(url.href);      // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-containerconsole.log(url.protocol);  // https:console.log(url.host);      // developer.mozilla.orgconsole.log(url.hostname);  // developer.mozilla.orgconsole.log(url.port);      // (blank - https assumes port 443)console.log(url.pathname);  // /en-US/searchconsole.log(url.search);    // ?q=URLconsole.log(url.hash);      // #search-results-close-containerconsole.log(url.origin);    // https://developer.mozilla.org

规范EDIT

Specification Status Comment
WHATWG HTML Living Standard
Location
Living Standard No change from HTML5.
HTML5
Location
Recommendation Initial definition.

浏览器兼容性EDIT


  • Desktop

  •  

  • Mobile

特性 Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
origin on Windows.location ? 21 (21) ? ? ?
origin on all location objects (but on Workers, that use WorkerLocation ? 26 (26) ? ? ?
username and password ? 26 (26) ? ? ?
searchParams ? 34 (34) ? ? ?


另见EDIT

  • Two methods creating such an object: Window.location and Document.location.

出处:https://developer.mozilla.org/zh-CN/docs/Web/API/Location

参考文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#location



你可能感兴趣的:(接口,信息)