jquery or JS get URL parameters

Source: Internet
Author: User

<script type= "Text/javascript" >functionGeturlparam (name) {varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)");//constructs a regular expression object that contains a target parametervarr = Window.location.search.substr (1). Match (REG);//Match target Parametersif(r!=NULL)returnUnescape (r[2]);return NULL;//Return parameter Values} Alert (Geturlparam ( ' cc '));</script>

Geturlparam This method is to get the URL after the URL through the regular to parse, when used, such as red Alert, CC is the name of the parameter

Understand the way JavaScript gets URL parameters, we can extend a method for jquery to get URL parameters through jquery, the following code expands a Geturlparam () method for jquery

(functionfunction(name) {varnew RegExp ("(^|&)" + name + "= ([^&]*) (&|$) "); var r = window.location.search.substr (1). Match (reg); if (r!=nullreturnreturnnull;}}) (jQuery);

After extending this method for jquery, we can get the value of a parameter in the following way

$.getUrlParam( ‘cid‘ );

If you're not sure how to add extension methods to jquery, you can refer to the jquery Plugin development tutorial

jquery or JS get URL 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.