1, what is window.location? Example
Url:http://b.a.com:88/index.php?name=kang&when=2011#first
| Properties |
meaning |
value |
| Protocol |
Agreement |
"http:" |
| Hostname |
The name of the server |
"B.a.com" |
| Port |
Port |
"88" |
| Pathname |
The part of the URL that is behind the host name |
"/index.php" |
| Search |
"?" The later part, also called the query string |
"? name=kang&when=2011" |
| Hash |
Returns the content after "#" |
"#first" |
| Host |
equals hostname + Port |
"B.a.com:88" |
| Href: |
The full URL of the current page |
"Http://www.a.com:88/index.php?name=kang&when=2011#first" |
Window.location and document.location are mutually equivalent and can be exchanged for use
The 8 properties of the location are both readable and writable, but only the href and hash write make sense. For example, changing the location.href will relocate to a URL, while modifying Location.hash will jump to the anchor (<a id= "name" > or <div id= "id" > etc.) name tag (if any) in the current page ), 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=" First "?" After the hash: "#when =2011#first" after the first "#" content
2, why is window.location.search empty?
A : Note the difference between the search and hash above, if the url "? "Before there was a" # "for example:"http://localhost:63342/index.html#/version? " type=35&id=5"Then use Window.location.search to get Empty (" "). Because "? type=35&id=5"string character is belong to"#/version? Type=35&id=5"This string of characters, that is, query string search can only be taken to"? "After and" # "before the content, if" # "before"? " "Search value is empty.
3, Application
1 //Get URL parameters2 functiongetquerystring (name)3 {4 varafter = Window.location.hash.split ("?") [1];5 if(after)6 {7 varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)");8 varR =After.match (reg);9 if(r! =NULL)Ten { One returndecodeURIComponent (r[2]); A } - Else - { the return NULL; - } - } -}
Why is Window.location.search empty?