下面通过一段代码给大家介绍js中获取URL参数的共用方法getRequest()方法,具体代码如下所示:
getRequest : function() { 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; }
下面看下js获取url参数值的两种方式详解
有个url如下:
http://passport.csdn.net/account/login"htmlcode">
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; } var from = getQueryString("from"); alert(from);
方法二:
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; } var req = GetRequest(); var from = req['from']; alert(from);
总结
以上所述是小编给大家介绍的js中获取URL参数的共用方法getRequest()方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!