1 function getQueryStringArgs() { 2 //取得查询字符串并去掉开头的问号 3 var qs = (location.search.length > 0 ? location.search.substring(1) : ""); 4 5 //保存数据对象 6 args = {}; 7 8 //取得每一项 9 items = qs.length ? qs.split("&") : [], 10 item = null; 11 name = null; 12 value = null; 13 14 //for循环 15 i = 0; 16 len = items.length; 17 18 //将每一项加入args对象中 19 for (i = 0; i < len; i++) { 20 item = items[i].split("="); 21 name = decodeURIComponent(item[0]); 22 value = decodeURIComponent(item[1]); 23 24 if (name.length) { 25 args[name] = value; 26 } 27 } 28 return args; 29 } 30 31 //假设 ?q=javascript&num=10 32 var args = getQueryStringArgs(); 33 alert(args["q"]); 34 alert(args["num"]);
书里面看来的