JavaScript splits the address bar parameters into key-value pairs.

Source: Internet
Author: User
Tags hash port number

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.