欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

获取url上的参数

程序员文章站 2022-06-24 15:26:15
var aa = '?name=hss&age=13'; function strToObj(str){ if(typeof str === 'undefined'){ str = location.search; } var reg = /[\?|&]+(\w+)=(\w*)/g; ... ......

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()


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