JS Get Get Parameters

Source: Internet
Author: User

For URLs, we need to know more because we might need to extract some of the information from the URL to do something different, in fact it's a unique way of interacting with the backend, which is certainly safe when the request is returned, the information about the URL is recorded in the Window object's In a Location object, it is important that the result of the value does not change as the user manually modifies the characters in the Address bar. To understand this, let's start with the following picture:

The long string in the middle of the picture is a full URL that contains any part of a URL that might contain: protocol, domain name, port number (of course, in most cases, we do not see the port number when browsing the Web page, because he is hidden, the default is 80 port, you add will not be wrong), path, parameter, stroke. In JavaScript, the way to get to this line of string is to access the Window.location.href,href property that contains the full URL of a page. If you want to get the value of a part of the URL, we can parse the full URL through a complex and tedious regular expression, but it's more convenient to get it through the other properties of the location. For example, the protocol attribute in location records the protocol name with a colon, and the pathname attribute stores the path name, which is the separation of the href, which brings a lot of convenience to the developer. The table below lists all the properties under location, along with their respective recorded values. If the contents of the table cannot be understood, then the color blocks and text in the above image depict the corresponding location of each property in the URL.

Properties value
Href The full URL
Protocol Agreement
Hostname Host Name
Host Host name plus port number
Port The port number
Pathname The path portion of the current URL
Search Query part of URL
Hash #开始的锚

It is important to note that the above properties are optional, which means that we can change their values through JavaScript. Most of these properties are very simple at a glance, only the search part is more troublesome, the search part is sent to the background parameters, to? Starting with,& as a delimiter, = Assigning a serialized string, so that the value obtained by Location.search is often not the final result to be obtained, and through the structure of search, we may want to get a result that is an associative array with a definite correspondence. So we need to do further processing of the strings that we get in Location.search.

12345678910111213141516171819 function getArgs() {    varargs = {};        varquery = location.search.substring(1);         // Get query string    varpairs = query.split("&");                    // Break at ampersand     for(vari = 0; i < pairs.length; i++) {            varpos = pairs[i].indexOf(‘=‘);             // Look for "name=value"            if(pos == -1) continue;                    // If not found, skip                var argname = pairs[i].substring(0,pos);// Extract the name                varvalue = pairs[i].substring(pos+1);// Extract the value                value = decodeURIComponent(value);// Decode it, if needed                args[argname] = value;                        // Store as a property        }    returnargs;// Return the object }

The code above comes from the "JavaScript authoritative guide" book. The Getargs method does not receive parameters, it actively extracts the search part of the URL to parse it, and returns a JSON. For example, the URL in the image we started with, using the Getargs method will get the following result:

123 varsearch = {    search:html}

In this way, all information about the URL can get a very clear result, very simple.

However, it is more recommended to use regular expressions to parse URLs, which are more efficient and more concise.

12345678910 functiongetArgs(){    varargs = {};    var match = null;    varsearch = decodeURIComponent(location.search.substring(1));    varreg = /(?:([^&]+)=([^&]+))/g;    while((match = reg.exec(search))!==null){        args[match[1]] = match[2];    }    returnargs;}

Original link: http://www.jsann.com/post/JS_GET_parameters_to_obtain.html

JS Get Get Parameters

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.