js获取url参数

人气:487 ℃/2023-01-12 07:28:21

在js下获取url参数的值是怎样的呢?下面就让我们一起来了解一下吧:

一般来说,使用js获取url中的某个参数值,可以通过将url的参数转换成数组形式,然后再通过for循环逐个查找数组元素,将参数值找出来,不过除了这种方法之外还有更简易的,也就是可以采用正则分析法。

参考范例:

方式一:

输入指令:

function getQueryString(name) {

var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");

var r = window.location.search.substr(1).match(reg);

if (r != null) return unescape(r[2]); return null;

}

调用:

alert(GetQueryString("参数名1"));

alert(GetQueryString("参数名2"));

alert(GetQueryString("参数名3"));

方式二:

输入指令:

<Script language="javascript">

function GetRequest() {

var url = location.search; //获取url中"?"符后的字串

var theRequest = new Object();

if (url.indexOf("?") != -1) {

var str = url.substr(1);

strs = str.split("&");

for(var i = 0; i < strs.length; i ++) {

theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);

}

}

return theRequest;

}

</Script>

调用:

<Script language="javascript">

var Request = new Object();

Request = GetRequest();

var 参数1,参数2,参数3,参数N;

参数1 = Request['参数1'];

参数2 = Request['参数2'];

参数3 = Request['参数3'];

参数N = Request['参数N'];

</Script>

以上就是小编的分享了,希望能够帮助到大家。

js获取url参数值的两种方式

jquery获取url参数及url加参数的方法

1、url 取值和加参数的方法

$(document).off('click','.js-editReload').on('click','.js-editReload',function(event){var $t=$(this), $newid=$t.attr("data-reload"),thisURL=window.location.href;if (thisURL.indexOf("markvid=") > 0) { var oldid=$.Request('markvid');//$.Request 是调用方法,获取地址栏目的参数 if (oldid != null) { thisURL=thisURL.replace("markvid=" oldid,"markvid=" $newid); } else { thisURL=thisURL.replace("markvid=","markvid=" $newid); }} else { if (thisURL.indexOf("?") > 0) { thisURL = thisURL "&markvid=" $newid; } else { thisURL = thisURL "?markvid=" $newid; }}// location.href = thisURL;thisChangeURL=thisURL; });2、jquery 取url参数和在url加参数(直接调用即可)

(function (e) { e.extend({ Request: function (e) { var n = location.search.match(new RegExp("[?&]" e "=([^&]*)(&?)", "i")); return n ? n[1] : n } })})(Jquery);//取值操作 www.baidu.com?act=1console.log($.Request("act")) // 13、Urldecode解码及UrlEncode编码的jQuery方法

地址栏中,若有中文,比如www.baidu.com?mid=ff芳芳控制台打印mid值为:ff芳芳可使用$('#name').text(decodeURI($.Request("mid")) );//ff芳芳

Jquery解码:decodeURIComponent(url);Jquery编码:encodeURIComponent(url);

推荐

首页/电脑版/网名
© 2026 NiBaKu.Com All Rights Reserved.