JavaScript Windows. Location object

  • 2020-03-30 04:18:51
  • OfStack

Often use window.location, its structure is always hard to remember, simple comb, convenient later query.

The sample
URL: (link: http://b.a.com:88/index.php? Name = kang&when = 2011 # first)

attribute meaning value
protocol: agreement "http:"
hostname: Server name "b.a.com"
port: port "88"
pathname: URL After the host name "/index.php"
search: "?" The latter part is also known as the query string "?name=kang&when=2011"
hash: return "#" After that "#first"
host: Is equal to the hostname + port "b.a.com:88"
href: The integrity of the current page 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

All eight properties of location are read-write, but only href and hash writes make sense. For example, changing location.href relocates to a URL, while changing location.hash jumps to the anchor in the current page (< A id = "name" > Or < Div id = "id" > Name tag (if any), and the page will not be reloaded

Pay attention to
URL: (link: http://b.a.com:88/index.php? Name = kang&how = # s = 2011 # first)

search: "?name=kang&how=" The first one "?" after
hash: "#when=2011#first" The first one "#" After that
Search: "& # 63; Name =kang&how=" first "?" after
Hash :"#when=2011#first" after the first" #"

methods

The location. The assign (url)
The location. The assign (' http://www.baidu.com '); Equivalent to window.location = 'http://www.baidu.com'
This puts the new address on the browser's history stack, meaning that the back button can still go back to the new page.
The location. The replace (url)
The same as the assign method, but removes the page from the browser history stack, meaning that the back button cannot return to the page after a jump to a new page. At present, IE, Chrome is a simple jump, only Firefox will delete the history of this page.
The location. Reload (force)
Reload the current page. Reload from the server when force is true; False is reloaded from the browser cache, with the default value false.


Related articles: