Get the requested URL parameter in JavaScript [regular]_javascript tips

Source: Internet
Author: User
First method:, code comparison professional recommendation
Copy Code code as follows:

<script>
function Getlocationparam (param) {
var request = {
Querystring:function (val) {
var uri = Window.location.search;
var re = new RegExp ("+val+" = ([^&?] *) "," IG ");
Return (Uri.match (re))? ( decodeURI (Uri.match (re) [0].substr (val.length+1)): "];
}
}
Return request. QueryString (param);
}
var uid=getlocationparam ("UID");
</script>

The second method:
Of course, we can get the value of the parameters in the background, and then in the foreground JS code to get the value of the variable, the specific way to refer to my article: JavaScript to get the background C # variables and invoke the background method.

In fact, we can also directly in JS to get the value of the requested parameter, by using Window.location.search can get to the current URL of the start of the string, such as the previous link to obtain the search for the id=001. The obtained string is then processed so that the value of the parameter can be obtained.
Copy Code code 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 you call the above method, you can get the value of the parameter you want, as long as you pass in the name of the parameter, such as Geturlparam ("id").
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.