JavaScript window. location object, javascriptwindow
Window. location is often used, and its structure is hard to remember. It can be easily sorted to facilitate future queries.
Example
URL: http:// B .a.com: 88/index. php? Name = kang & when = 2011 # first
Attribute |
Description |
Value |
Protocol: |
Protocol |
"Http :" |
Hostname: |
Server Name |
"B .a.com" |
Port: |
Port |
"88" |
Pathname: |
The part after the host name in the URL |
"/Index. php" |
Search: |
"? ", Also known as the query string |
"? Name = kang & when = 2011" |
Hash: |
Returned content after "#" |
"# First" |
Host: |
Equal to hostname + port |
"B .a.com: 88" |
Href: |
Complete URL of the current page |
"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 in exchange.
The eight attributes of location can be read and written, but only href and hash write make sense. For example, change location. href locates a URL and modifies the location. hash will jump to the anchor (<a id = "name"> or <div id = "id">) name tag on the current page (if any ), and the page will not be reloaded.
Note:
URL: http:// B .a.com: 88/index. php? Name = kang & how = # when = 2011 # first
Search: |
"? Name = kang & how =" |
First "? After |
Hash: |
"# When = 2011 # first" |
Content after the first "#" |
Method
-
Location. assign (url)
-
Location. assign ('HTTP: // www.baidu.com '); equivalent to window. location = 'HTTP: // www.baidu.com'
In this way, the new address will be stored in the browser history stack, which means that after the new page is transferred, the "back" button can still return to the page.
-
Location. replace (url)
-
The method is the same as the assign method, but the page is deleted from the browser history stack. That is to say, the page cannot be returned after the "back" button is redirected to the new page. Currently, IE and Chrome are simple jumps. Only Firefox deletes the history of this page.
-
Location. reload (force)
-
Reload the current page. If force is set to true, It is reloaded from the server. If false, it is reloaded from the browser cache. The default value is false.
Invalid javascript windowlocation object?
Open the html file locally. The value of window. location. host is empty. Of course there is no value.
You can try alert (window. location. host;
Javascript location object
The default port of the http protocol. For example, if you access www.baidu.com: 80, it is the same as accessing www.baidu.com.
Location. hash can be used to obtain or set the tag value of the page.