Home > Web Front-end > JS Tutorial > How to get url parameters and url plus parameters in jquery_jquery

How to get url parameters and url plus parameters in jquery_jquery

WBOY
Release: 2016-05-16 15:35:13
Original
2962 people have browsed it

Using jquery to obtain url and using jquery to obtain url parameters are operations we often use. The following is an analysis of it in the form of text description and code analysis. Please see below for specific details.

1. It is very simple to obtain the url with jquery. The code is as follows:

Copy code The code is as follows:

window.location.href;

In fact, it only uses the basic window object of javascript and does not use the knowledge of jquery.

2. Getting url parameters with jquery is more complicated and requires the use of regular expressions, so it is important to learn javascript regular expressions well

First, let’s take a look at how to get a certain parameter in the URL simply through javascript:

  //获取url中的参数
  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; //返回参数值
  }
Copy after login

You can get the value of the parameter by passing the parameter name in the url through this function. For example, the url is

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

We want to get the value of reurl, we can write like this:

Copy code The code is as follows:

var xx = getUrlParam('reurl');

Understand the method of javascript to obtain url parameters. We can use this method to extend a method for jquery to obtain url parameters through jquery. The following code extends 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);
Copy after login

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

Copy code The code is as follows:

var xx = $.getUrlParam('reurl');

Full code:

 
 

Copy after login

2014-4-23 修改

今天在用上面的方法获取url中的参数时,url中传递的中文参数在解析的时候无论怎么测试,获取的都是乱码。经过一番调试后发现,我再传递参数时,对汉字编码使用的是 encodeURI ,而上面的方法在解析参数编码时使用的是unescape ,修改为 decodeURI 就可以了。

附: W3School中的介绍:

JavaScript unescape() 函数

unescape() 函数可对通过 escape() 编码的字符串进行解码。

参数 描述
string 必需。要解码或反转义的字符串。

说明

该函数的工作原理是这样的:通过找到形式为 %xx 和 %uxxxx 的字符序列(x 表示十六进制的数字),用 Unicode 字符 \u00xx 和 \uxxxx 替换这样的字符序列进行解码。

提示和注释

注释:ECMAScript v3 已从标准中删除了 unescape() 函数,并反对使用它,因此应该用 decodeURI() 和 decodeURIComponent() 取而代之。

综上: javascript对参数编码解码方法要一致:

escape() unescape()

encodeURI() decodeURI()

encodeURIComponent() decodeURIComponent()

网上找的另一种javascript获取url中参数的方法:

<script language="JavaScript" type="text/javascript"> 
function GetUrlParms() 
{
 var args=new Object(); 
 var query=location.search.substring(1);//获取查询串 
 var pairs=query.split("&");//在逗号处断开 
 for(var i=0;i<pairs.length;i++) 
 { 
  var pos=pairs[i].indexOf('=');//查找name=value 
   if(pos==-1) continue;//如果没有找到就跳过 
   var argname=pairs[i].substring(0,pos);//提取name 
   var value=pairs[i].substring(pos+1);//提取value 
   args[argname]=unescape(value);//存为属性 
 }
 return args;
}
var args = new Object();
args = GetUrlParms();
//如果要查找参数key:
if(args["id"]!=undefined)
{
//如果要查找参数key:
var value1 = args["id"] ;
alert(value1);
}</script>
Copy after login

jquery 取url参数和在url加参数

(function ($) {
 $.extend({
  Request: function (m) {
   var sValue = location.search.match(new RegExp("[\&#63;\&]" + m + "=([^\&]*)(\&&#63;)", "i"));
   return sValue &#63; 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("[\&#63;]")) {
      r = url + "&" + tmp;
     } else {
      r = url + "&#63;" + tmp;
     }
    }
   }
   return r;
  }

 });
})(jQuery);
Copy after login

使用方法

dev.zhang.com/IOF.Signup/index_uscn_chs.html?act=1

1、取值使用

$.Request("act") = 1

2、url加参数

$.UrlUpdateParams(window.location.href, "mid", 11111),

结果window.location.href?mid=11111

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template