频道栏目
首页 > 资讯 > 其他 > 正文

常见编码转中文,获取url参数

17-01-07        来源:[db:作者]  
收藏   我要投稿

1.unicode编码转中文 例如‘&#36865&#30340&#21457&#36865&#21040’

G.globalHtmlCharDecode = function(obj) {
        var type = function(obj) {
            return Object.prototype.toString.call(obj);
        },

        isString = function(obj) {
            return type(obj) === '[object String]'
        },

        isObj = function(obj) {
            return type(obj) === '[object Object]'
        },

        isArr = function(obj) {
            return type(obj) === '[object Array]'
        },

        decode = function(str) {
            var div = document.createElement('div')
            div.innerHTML = str;
            str = div.innerText;
            div = null;
            return str;
        },

        objFn = function(origin, target) {
            for ( var i in origin) {
                // if(Object.hasOwnProperty(i))
                oi = origin[i];

                if (i.indexOf('&#') > -1) {
                    i = decode(decode(i))
                }
                target[i] = oi;

                if (isString(oi) && oi.indexOf('&#') > -1) {
                    target[i] = decode(decode(oi))
                } else if (isObj(oi) || isArr(oi)) {
                    objFn(oi, target[i] = (isObj(oi) ? {} : []))
                }
            }

            return target;
        }
        if (isObj(obj) || isArr(obj)) {
            obj = objFn(obj, isObj(obj) ? {} : []);
        } else if (isString(obj)) {
            obj = decode(obj)
        } else {
            // throw new Error('globalHtmlCharDecode params error')
        }

        return obj;
    }
浏览器url截取如果中文 类似 %3e%%dd%
 this.appsubname = decodeURIComponent(G.getUrlParam('str'));
截取url参数
//获取参数
    G.getUrlParam = function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return r[2]; return '';
    }
相关TAG标签
上一篇:用 Servlet 进行上载的原理和实现
下一篇:TypeScript错误“propertydoesnotexistonvalueoftype”
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站