functiongetQueryString(name){var reg =newRegExp('(^|&)'+ name +'=([^&]*)(&|$)','i');var r = window.location.search.substr(1).match(reg);if(r !=null){returnunescape(r[2]);}returnnull;}// 这样调用:alert(GetQueryString("参数名"));
方法二:split拆分法
functionGetRequest(){var url = location.search;//获取url中"?"符后的字串var theRequest =newObject();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 Request =newObject();
Request =GetRequest();// var 参数1,参数2,参数3,参数N;// 参数1 = Request['参数1'];// 参数2 = Request['参数2'];// 参数3 = Request['参数3'];// 参数N = Request['参数N'];
方法三:正则(推荐使用)
functionGetQueryString(name){var reg =newRegExp("(^|&)"+ name +"=([^&]*)(&|$)","i");var r = window.location.search.substr(1).match(reg);//获取url中"?"符后的字符串并正则匹配var context ="";if(r !=null)
context =decodeURIComponent(r[2]);
reg =null;
r =null;return context ==null|| context ==""|| context =="undefined"?"": context;}alert(GetQueryString("q"));