因经常使用路由的hash值传参,便对其封装了函数,如下直接调用即可
1 (function getHashStringArgs() { 2 //取得查询的hash后?的位置 3 var position = window.location.hash.indexOf("?"); 4 // 截取?后面的字符串 5 var hashStrings = (window.location.hash.length > 0 ? window.location.hash.substring(position+1) : ""), 6 // 用来保存分割的hash对 7 hashArgs = {}, 8 //取得每一项hash对 9 items = hashStrings.length > 0 ? hashStrings.split("&") : [], 10 item = null, 11 name = null, 12 value = null, 13 i = 0, 14 len = items.length; 15 //逐个将每一项添加到hashArgs中 16 17 for (i = 0; i < len; i++) { 18 19 item = items[i].split("="); 20 21 name = decodeURIComponent(item[0]); 22 23 value = decodeURIComponent(item[1]); 24 25 if (name.length > 0) { 26 27 hashArgs[name] = value; 28 29 } 30 31 } 32 33 return hashArgs; 34 35 })()