Home >Web Front-end >JS Tutorial >JavaScript window.location object_Basics
I often use window.location, but I can’t remember its structure. Let’s briefly sort it out to facilitate future inquiries.
Example
URL: http://b.a.com:88/index.php?name=kang&when=2011#first
属性 | 含义 | 值 |
---|---|---|
protocol: | 协议 | "http:" |
hostname: | 服务器的名字 | "b.a.com" |
port: | 端口 | "88" |
pathname: | URL中主机名后的部分 | "/index.php" |
search: | "?"后的部分,又称为查询字符串 | "?name=kang&when=2011" |
hash: | 返回"#"之后的内容 | "#first" |
host: | 等于hostname port | "b.a.com:88" |
href: | 当前页面的完整URL | "http://www.a.com:88/index.php?name=kang&when=2011#first" |
window.location and document.location are equivalent to each other and can be used interchangeably
The eight attributes of location are all readable and writable, but only the writing of href and hash is meaningful. For example, changing location.href will relocate to a URL, and changing location.hash will jump to the name of the anchor ( or Attention
Method location.assign( url )
URL: http://b.a.com:88/index.php?name=kang&how=#when=2011#first
search:"?name=kang&how="After the first "?"
search:
"?name=kang&how="
第一个"?"之后
hash:
"#when=2011#first"
第一个"#"之后的内容
hash:"#when=2011#first"The content after the first "#"
location.assign('http://www.baidu.com'); is equivalent to window.location = 'http://www.baidu.com'
This method will put the new address into the browser history stack, which means that after going to a new page, the "back button" can still return to that page.
location.replace( url )
The same as the assign method, but this page will be deleted from the browser history stack, which means that the "back button" cannot return to the page after jumping to a new page. Currently, IE and Chrome simply jump, and only Firefox will delete the history of this page.
location.reload( force )
Reload the current page. When force is true, it is reloaded from the server; if false, it is reloaded from the browser cache. The default value is false.