1 function request(paras) { 2 var url = location.href; 3 var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&"); 4 var paraObj = {} 5 for (i = 0; j = paraString[i]; i++) { 6 paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length); 7 } 8 var returnValue = paraObj[paras.toLowerCase()]; 9 if (typeof (returnValue) == "undefined") {10 return "";11 } else {12 return returnValue;13 }14 }15 16 17 18 var facadeInteriorResourceInfoID = request("facadeInteriorResourceInfoID");19 20 var facadeInteriorImageID = request("facadeInteriorImageID");
另外一种方式,效率更高些,且更完善(来自JavaScript高级编程):
1 function getQueryStringArgs() { 2 var qs = (location.search.length > 0 ? location.search.substring(1) : ""), 3 args = {}, 4 items = qs.length ? qs.split("&") : [], 5 item = null, 6 name = null, 7 value = null, 8 i = 0, 9 len = items.length;10 11 for (i = 0; i < len; i++) {12 item = items[i].split("=");13 name = decodeURIComponent(item[0]);14 value = decodeURIComponent(item[1]);15 16 if (name.length) {17 args[name] = value;18 }19 }20 21 return args;22 }23 24 //查询字符串为?q=javascript&num=1025 var args = getQueryStringArgs();26 alert(args["q"]); //"javascript"27 alert(args["num"]);//"10"