Window. location can obtain a series of information in the address bar, and each browser supports this attribute, which is very convenient. The parameters after the obtained question mark can be processed into the desired key-value pair.
Location attributes
Attribute name |
Example |
Description |
Hash |
# Contents |
Returns the hash (# followed by zero or multiple characters) of the URL. If the URL does not contain a hash, an empty string is returned. |
Host |
Www.wrox.com: 80 |
Returns the server name and port number (if any) |
Hostname |
Www.wrox.com |
Returns the name of the server without a port number. |
Href |
Http://www.wrox.com |
Returns the complete URL of the currently loaded page. The toString () method of the location object also returns this value. |
Pathname |
WileyCDA |
Returns the directory or file name in the URL. |
Port |
8080 |
Returns the port number specified in the URL. If the URL does not contain the port number, an empty string is returned. |
Protocol |
Http: |
Return the usage protocol of the page. Usually http: or https: |
Search |
? Q = javascript |
Returns the query string of the URL, which starts with a question mark. |
If you modify any of these attributes, the page will be refreshed. Of course, there are other ways to refresh the page.
Location refresh
Location. assign (url); // jump link
Location. href = url; // jump link
Window. location = url; // jump link
Location. replace (url); // link, not saved in history
Location. reload (); // refresh, read from the cache
Location. reload (true); // refresh and read from the server again
Get the parameters in the address bar and display them as key-value pairs
Function getQueryStringArgs (){
Var qs = (location. search. length> 0? Location. search. substring (1 ):""),
Args = {},
Items = qs. length? Qs. split ("&"): [],
Item = null,
Name = null,
Value = null,
I = 0,
Len = items. length;
For (I = 0; I <len; I ++ ){
Item = items [I]. split ("= ");
// DecodeURIComponent decoding
Name = decodeURIComponent (item [0]);
Value = decodeURIComponent (item [1]);
If (name. length ){
Args [name] = value;
}
}
Return args;
}
You can call the getQueryStringArgs () method to return the parameter information in the address bar and save it in the object.