JS gets its value based on the key name in the URL query string

Source: Internet
Author: User

To run the results first

Here is the page code

Testjsgeturlattribute.html

<script>varRout = Geturlattribute (' rout ');if(NULL!=rout) {alert (rout);}/** * 20150514 14:30 * Ro * gets its value based on the key name in the URL query string */ function geturlattribute(paramename) {    //location.search is a string that starts with the current URL's?, which is the query string    varquery = (Location.search.length >0? Location.search.substring (1) :NULL);if(NULL!=query) {varargs =New Object( );varPairs = Query.split ("&"); for(vari =0; i < pairs.length; i++) {varpos = Pairs[i].indexof ("=");if(pos = =-1)Continue;varArgname = pairs[i].substring (0, POS);varValue = pairs[i].substring (pos+1); Value =decodeuricomponent(value);         Args[argname] = value; }//Get value based on key name        returnArgs[paramename]; }return NULL;}</script>

The principle is to use the location object in JavaScript, which provides information about the document loaded in the current window, and provides some navigational features.
The Location object is a very special object because it is both a property of the Window object and a property of the Document object.
Window.location and Document.location refer to the same object.
The purpose of a Location object is not only to preserve the information of the current document, but also to parse the URL into separate fragments, which can be accessed through different properties.

The following table is all the properties of the Location object:

Although you can use the above properties to access most of the information for a location object, it is not convenient to access the properties in the query string that the URL contains.
Although Location.search returns everything from the question mark to the end of the URL, there is no way to access each of these query string parameters individually.

So, I created the function above to parse the query string and return its value based on the key name.

JS gets its value based on the key name in the URL query string

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.