var parseQueryString = function (url) { var reg_url = /^[^?]+?([wW]+)$/, reg_para = /([^&=]+)=([wW]*?)(&|$)/g, //g is very important arr_url = reg_url.exec(url), ret = {}; if (arr_url && arr_url[1]) { var str_para = arr_url[1], result; while ((result = reg_para.exec(str_para)) != null) { ret[result[1]] = result[2]; } } return ret; }
//使用方法: var url = "key0=0&key1=1&key2=&key3=http://www.g.cn?a=1&&b=2"; var obj = parseQueryString(url); console.dir(obj);
/** * description:获取URL的参数的value *@name:参数名称 */ function GetUrlParam(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; //返回参数值 }
//传参数之前对中文的url参数进行 window.location = encodeURI(encodeURI("xxx.html?title="+"中文")); //接收参数的地方 decodeURI(GetUrlParam('title'));
技术交流QQ群:15129679