首页 > 其他 > 详细

地址hash值分割处理函数

时间:2016-11-14 20:34:17      阅读:302      评论:0      收藏:0      [点我收藏+]

  因经常使用路由的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         })()

 

地址hash值分割处理函数

原文:http://www.cnblogs.com/worldly1013/p/6063018.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!