jquery gets url parameter and URL plus parameter method _jquery

Source: Internet
Author: User
Tags extend

Using jquery to get URLs and using jquery to get URL parameters is the operation we often use, the following text description and code analysis of the form for everyone to analyze, please see below for details.

1, jquery get URL is very simple, the code is as follows:

Copy Code code as follows:

Window.location.href;

Actually just use JavaScript to base the Window object, and not with the knowledge of jquery.

2, jquery get URL parameters more complex, to use regular expressions, so learn JavaScript regular style how important things

First look at how the simple JavaScript is going to get a parameter in the URL:

  Gets the parameter
  function Geturlparam (name) {
   var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)") in the URL; A regular expression object containing a target parameter,
   var r = window.location.search.substr (1). Match (reg);//Match target parameter
   if (r!= null) return unescape ( R[2]); return null; Return parameter Value
  }

Passing the parameter name in the URL through this function can get the value of the parameter, such as the URL

Http://localhost:33064/WebForm2.aspx?reurl=WebForm1.aspx

We're going to get the value of Reurl, and we can write this:

Copy Code code as follows:

var xx = Geturlparam (' Reurl ');

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

  (function ($) {
    $.geturlparam = function (name) {
     var reg = new RegExp (^|&) "+ name +" = ([^&]*) (&|$) ");
     var r = window.location.search.substr (1). Match (reg);
     if (r!= null) return unescape (r[2]); return null;
    }
   }) (JQuery);

After extending this method for jquery, we can get the value of a parameter by using the following method:

Copy Code code as follows:

var xx = $.geturlparam (' Reurl ');

Complete code:

 <script src= "Js/jquery-1.7.2.min.js" type= "Text/javascript" ></script>
 <script type= "text/" JavaScript ">
  $ (function () {
   //Method Two:
   (function ($) {
    $.geturlparam = function (name) {
     var reg = NE W RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
     var r = window.location.search.substr (1). Match (reg);
     if (r!= null) return unescape (r[2]); return null;
    }
   }) (jQuery);
   Method two:
   var xx = $.geturlparam (' Reurl ');
   Method One:
   //var xx = Geturlparam (' Reurl ');

   Alert (xx);
  });
  Method One:
  //Get parameter
  function Geturlparam (name) {
   var reg = new RegExp (^|&) + name + = ([^&]*) amp;|$) "); Constructs a regular expression object containing the target parameter
   var r = window.location.search.substr (1). Match (reg);//Match target parameter
   if (r!= null) return Unescape (r[2]); return null; Return parameter Value
  }
 </script>

2014-4-23 modified

Today in the above method to get the parameters in the URL, the URL passed in the Chinese parameters in the parsing of the time no matter how the test, get the garbled. After some debugging found that I passed the parameters, the use of encoding is encodeURI, and the above method in the resolution of the parameters of the code used is unescape, modified to decodeURI can be.

Attached: W3school in the introduction:

JavaScript unescape () function

The unescape () function decodes a string encoded by escape ().

Parameters Description
String Necessary. The string to decode or reverse.

Description

This function works by finding a sequence of characters in the form%xx and%uxxxx (x represents a hexadecimal number), which is decoded by replacing such sequences with Unicode characters \u00xx and \uxxxx.

Tips and comments

Note: ECMAScript v3 has removed the unescape () function from the standard and objected to its use, so it should be replaced with decodeURI () and decodeURIComponent ().

In summary: JavaScript is consistent with the parameter encoding and decoding method:

Escape () unescape ()

encodeURI () decodeURI ()

encodeURIComponent () decodeuricomponent ()

Find another kind of javascript on the Internet method to get the parameters in the URL:

<script language= "JavaScript" type= "text/javascript" > 
function geturlparms () 
{
 var args=new Object (); 
 var query=location.search.substring (1);//Get query string 
 var pairs=query.split ("&")//Disconnect for 
 (var i=0;i< at comma) pairs.length;i++) 
 { 
  var pos=pairs[i].indexof (' = ');//Find Name=value 
   if (pos==-1) continue;//Skip if not found 
   var argname=pairs[i].substring (0,pos);//extract name 
   var value=pairs[i].substring (pos+1);//Extract Value 
   Args[argname]=unescape (value);//Save As Property 
 } return
 args;
}
var args = new Object ();
args = Geturlparms ();
If you want to find the parameter key: if
(args["id"]!=undefined)
{
//If you want to find the parameter key:
var value1 = args["id"];
alert (value1);
} </script>

jquery takes URL parameters and adds parameters at URL

(function ($) {
 $.extend ({
  request:function (m) {
   var svalue = Location.search.match (New RegExp ("[\?\& ] "+ M +" = ([^\&]*) (\&?) "," I "));
   Return svalue? SVALUE[1]: svalue
  },
  urlupdateparams:function (URL, name, value) {
   var r = URL;
   if (r!= null && r!= ' undefined ' && r!= "") {
    value = encodeuricomponent (value);
    var reg = new RegExp ("(^|)" + name + "= ([^&]*) (|$)");
    var tmp = name + "=" + value;
    if (Url.match (reg)!= null) {
     r = Url.replace (eval (reg), TMP);
    else {
     if (url.match ("[\?]")) {
      R = url + ' & ' + tmp;
     } else {
      R = url + "?" + tmp;
     }
   }} return r;
  }

 });
(JQuery);

How to use

Dev.zhang.com/iof.signup/index_uscn_chs.html?act=1

1, take the value use

$. Request ("act") = 1

2, URL plus parameters

$. Urlupdateparams (Window.location.href, "mid", 11111),

Results window.location.href?mid=11111

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.