Summary: Methods for adding URL parameters to the client and server side using regular

Source: Internet
Author: User

First, the server-side:

C # version

#regionURL parameter action/// <summary>    ///URL parameter Action/// </summary>     Public classUrlpagehelper {/// <summary>        ///add a URL parameter in regular-expression mode/// </summary>        /// <param name= "key" ></param>        /// <param name= "value" ></param>        /// <returns></returns>         Public Static stringAddurlparm (stringKeystringvalue) {            stringURL =HttpContext.Current.Request.RawUrl; URL= Regex.Replace (Url +"&",@"(.*)(\?| &)"+ key +"=[^&]+? (&) (.*)","$1$2$4", regexoptions.ignorecase); URL= Url.remove (Url.length-1); if(Url.contains ('?') ) {Url=string. Format ("{0}&{1}={2}", URL, key, value); }            Else{URL=string. Format ("{0}? {1}={2}", URL, key, value); }            returnUrl; }    }    #endregion

PHP version

/** * Add URL parameter * @param string $url original URL * @param string $key key * @param string $value value * @retu RN string New URL*/    Private functionAdd_querystring_var ($key,$value) {        $url=$this-URL; $url=Preg_replace('/(. *) (\?| &) '.$key.‘ =[^&]+? (&) (. *)/I ', ' $1$2$4 ',$url.‘ & '); $url=substr($url, 0,-1); if(Strpos($url,‘?‘) ===false){            return($url.‘?‘.$key.‘ = '.$value); } Else {            return($url.‘ & '.$key.‘ = '.$value); }    }

Then the client's

(function($) {$.extend ({Request:function(m) {varsvalue = Location.search.match (NewRegExp ("[\?\&]" + M + "= ([^\&]*) (\&?)", "I")); returnSvalue? Svalue[1]: svalue; }, Urlupdateparams:function(URL, name, value) {varR =URL; if(r! =NULL&& r! = ' undefined ' && r! = "") {Value=encodeURIComponent (value); varReg =NewRegExp ("(^|)" + name + "= ([^&]*) (|$)"); varTMP = name + "=" +value; if(Url.match (reg)! =NULL) {R=Url.replace (eval (reg), TMP); }                Else {                    if(Url.match ("[\?]") ) {R= URL + "&" +tmp; } Else{R= URL + "?" +tmp; }                }            }            returnR; }    });}) (jQuery);

How to use the client:

$. Urlupdateparams (Window.location.href, "Order", "111")

Summary: Methods for adding URL parameters to the client and server side using regular

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.