Home >Web Front-end >JS Tutorial >JavaScript window.location object_Basics

JavaScript window.location object_Basics

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:31:251680browse

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

, etc.) in the current page. tag (if any), and the page will not be reloaded

Attention
URL:
http://b.a.com:88/index.php?name=kang&how=#when=2011#first

search: "?name=kang&how=" 第一个"?"之后
hash: "#when=2011#first" 第一个"#"之后的内容
search:"?name=kang&how="After the first "?"
hash:"#when=2011#first"The content after the first "#"

Method

location.assign( url )
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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn