星空网 > 软件开发 > Java

使用JavaScript获取URL中的参数(两种方法)

本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧。

方法一:

//取url参数 var type = request("type")function request() {var query = location.search;var paras = arguments[0];if (arguments.length == 2) {query = arguments[1];}if (query != "") {if (query.indexOf("?") != -1) {query = query.split("?")[1];}query = query.split("&");for (var i = 0; i < query.length; i++) {var querycoll = query[i].split("=");if (querycoll.length == 2) {if (querycoll[0].toUpperCase() == paras.toUpperCase()) {return querycoll[1];break;}}}}return "";}//调用方法var mid=request("mid");

  




方法二:正则法

unction request(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(request("mid"));

 





原标题:使用JavaScript获取URL中的参数(两种方法)

关键词:JavaScript

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Lazada推行全新的卖家违规扣分机制!:https://www.ikjzd.com/articles/15318
福州美国商标注册费用:https://www.ikjzd.com/articles/153180
​公司美国商标注册流程及费用:https://www.ikjzd.com/articles/153181
广州美国商标注册报价:https://www.ikjzd.com/articles/153185
​广州美国商标注册多少钱:https://www.ikjzd.com/articles/153186
​广州美国商标注册费用:https://www.ikjzd.com/articles/153187
大连有啥好玩的景点-大连有啥好玩的景点推荐孩子:https://www.vstour.cn/a/411235.html
咸阳机场到临潼景点 西安咸阳机场到临潼有多远:https://www.vstour.cn/a/411236.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流