获取url上的参数

var aa = '?name=hss&age=13';
        function strToObj(str){
            if(typeof str === 'undefined'){
                str = location.search;
            }
            var reg = /[\?|&]+(\w+)=(\w*)/g;
            var param = {};
            do{
                var item = reg.exec(str);
                if(item !== null){
                    param[item[1]] = item[2]
                }
            }while(reg.lastIndex > 0)
            return param;
        }
        console.time()
        console.log(strToObj());
        console.timeEnd()


执行效率还是蛮高的大概在一毫秒吧

posted on 2018-10-17 17:25  huo2song  阅读(245)  评论(0)    收藏  举报

导航