URL 中文乱码 分割成对应的数据;

1.  例如:  <a href="content.html?BtnType=查看&BtnTypeData=[{name:"xiao",{sex:"男"}}]"; 怎么获取中文而不是乱码呢  在这里看如下代码:

 2. 先进行乱码: <a href="content.html?BtnType=encodeURI("查看")&BtnTypeData=encodeURI([{name:"xiao",{sex:"男"}}]);  这样可以反而看到数据;进行加密可以看到乱码看不到真实数据;

3. 在进行解码  : 使用  decodeURI ()  函数 解码;

 var LoctionHref=decodeURI(decodeURI(location.href));// 获取网址
         var BtnType="";
         var BtnTypeData="";
         if (LoctionHref.indexOf("?") != -1) { // 判断是否有 ? .
                var str = LoctionHref.substr(1); 
                strs = str.split("?")[1]; // 按 ? 分割成数组 获取 1 中的数据
                strs = strs.split("&"); // 按 & 分割成数组 
                if(strs.length>1){ 
                    BtnType=strs[0].split("=")[1]; // 按 & 分割数组 0 序号 获取数据
                    BtnTypeData=strs[1].split("=")[1];//按 & 分割数组 1 序号 获取数据
                }else{
                    BtnType=strs[0].split("=")[1];// 按 & 分割数组 0 序号 获取数据
                }
            }

猜你喜欢

转载自blog.csdn.net/qq_38366657/article/details/81240810