Obtain the request URL parameters in JavaScript

Source: Internet
Author: User

Of course, we can get the parameter value in the background, and then get the variable value in the front-end js Code. For details, refer to my article: javaScript obtains the C # variable in the background and calls the background method.

In fact, we can also directly obtain the value of the Request Parameter in js. By using window. location. search, we can get the current URL? A string starting with "#". For example, the search result obtained from the previous link is? Id = 001. Then process the obtained string to obtain the parameter value.
Copy codeThe Code is as follows:
Function getUrlParam (name ){
Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $ )");
Var r = window. location. search. substr (1). match (reg );
If (r! = Null)
Return unescape (r [2]);
Return null;
}

When calling the preceding method, you only need to input the parameter name to obtain the value of the parameter you want, for example, getUrlParam ("id ").

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.