定位
接口表示链接到它的对象位置 URL。对它所做的改变会反映到它相关的对象。
Document
and
Window
接口有这样的链接
定位
,可访问凭借
Document.location
and
Window.location
分别。
<span id="href" title="href"><span id="protocol" title="protocol">http:</span>//<span id="host" title="host"><span id="hostname" title="hostname">example.org</span>:<span id="port" title="port">8888</span></span><span id="pathname" title="pathname">/foo/bar</span><span id="search" title="search">?q=baz</span><span id="hash" title="hash">#bang</span></span>
html, body {height:100%;}
html {display:table; width:100%;}
body {display:table-cell; text-align:center; vertical-align:middle; font-family:georgia; font-size:230%; line-height:1em; white-space:nowrap;}
[title] {position:relative; display:inline-block; box-sizing:border-box; /*border-bottom:.5em solid;*/ line-height:2em; cursor:pointer;}
[title]:before {content:attr(title); font-family:monospace; position:absolute; top:100%; width:100%; left:50%; margin-left:-50%; font-size:40%; line-height:1.5; background:black;}
[title]:hover:before,
:target:before {background:black; color:yellow;}
[title] [title]:before {margin-top:1.5em;}
[title] [title] [title]:before {margin-top:3em;}
[title]:hover,
:target {position:relative; z-index:1; outline:50em solid rgba(255,255,255,.8);}
[].forEach.call(document.querySelectorAll('[title][id]'), function (node) {
node.addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
window.location.hash = '#' + $(this).attr('id');
});
});
[].forEach.call(document.querySelectorAll('[title]'), function (node) {
node.addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
window.location.hash = '';
});
});
Location.ancestorOrigins
DOMStringList
containing, in reverse order, the origins of all ancestor browsing contexts of the document associated with the given
定位
对象。
Location.href
USVString
containing the entire URL. If changed, the associated document navigates to the new page. It can be set from a different origin than the associated document.
Location.protocol
USVString
包含 URL 的协议方案,包括最终的
':'
.
Location.host
USVString
包含主机,即
hostname
,
':'
,和
port
of the URL.
Location.hostname
USVString
包含 URL 的域。
Location.port
USVString
包含 URL 的端口号。
Location.pathname
USVString
包含初始
'/'
followed by the path of the URL.
Location.search
USVString
包含
'?'
followed by the parameters or "querystring" of the URL. Modern browsers provide
URLSearchParams
and
URL.searchParams
to make it easy to parse out the parameters from the querystring.
Location.hash
USVString
包含
'#'
followed by the fragment identifier of the URL.
Location.origin
只读
USVString
containing the canonical form of the origin of the specific location.
Location.assign()
Loads the resource at the URL provided in parameter.
Location.reload()
重新加载当前 URL,像刷新按钮。
Location.replace()
assign()
method and setting the
href
property is that after using
replace()
the current page will not be saved in session
历史
, meaning the user won't be able to use the
back
button to navigate to it.
Location.toString()
USVString
containing the whole URL. It is a synonym for
HTMLHyperlinkElementUtils.href
, though it can't be used to modify the value.
// 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.location
var url = document.createElement('a');
url.href = 'https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container';
console.log(url.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
console.log(url.protocol); // https:
console.log(url.host); // developer.mozilla.org:8080
console.log(url.hostname); // developer.mozilla.org
console.log(url.port); // 8080
console.log(url.pathname); // /en-US/search
console.log(url.search); // ?q=URL
console.log(url.hash); // #search-results-close-container
console.log(url.origin); // https://developer.mozilla.org:8080
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
在该规范中的 Location 定义。 |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
定位
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
ancestorOrigins
|
Chrome 20 | Edge 79 | Firefox No | IE No | Opera 15 | Safari 6 | WebView Android 4.4 | Chrome Android 25 | Firefox Android No | Opera Android 14 | Safari iOS 6 | Samsung Internet Android 1.5 |
assign
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 3 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
hash
|
Chrome Yes | Edge 12 | Firefox 22 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
host
|
Chrome Yes | Edge 12 | Firefox 22 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
hostname
|
Chrome Yes | Edge 12 | Firefox 22 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
href
|
Chrome Yes | Edge 12 | Firefox 22 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
origin
|
Chrome Yes | Edge 12 |
Firefox
26
|
IE
11
|
Opera 10 |
Safari
Yes
|
WebView Android Yes | Chrome Android Yes |
Firefox Android
26
|
Opera Android ? | Safari iOS 5 | Samsung Internet Android Yes |
password
弃用
非标
|
Chrome No | Edge No | Firefox 26 — 45 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 26 — 45 | Opera Android No | Safari iOS No | Samsung Internet Android No |
pathname
|
Chrome Yes | Edge 12 |
Firefox
22
|
IE
Yes
|
Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes |
Firefox Android
22
|
Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
port
|
Chrome Yes | Edge 12 | Firefox 22 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
协议
|
Chrome Yes | Edge 12 | Firefox 22 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
reload
|
Chrome 1 |
Edge
12
|
Firefox 1 |
IE
5.5
|
Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
replace
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
search
|
Chrome Yes | Edge 12 |
Firefox
22
|
IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes |
Firefox Android
22
|
Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
toString
|
Chrome 52 | Edge 12 | Firefox 22 |
IE
11
|
Opera ? | Safari ? | WebView Android 52 | Chrome Android 52 | Firefox Android 22 | Opera Android ? | Safari iOS ? | Samsung Internet Android 6.0 |
username
弃用
非标
|
Chrome No | Edge No | Firefox 26 — 45 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 26 — 45 | Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持
兼容性未知
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
Window.location
and
Document.location
.
URL
,
URLSearchParams
and
HTMLHyperlinkElementUtils
定位
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers