获取url中的参数

getQueryString(name) {
        //var search = window.location.search;   //注意的地方
        var position = window.location.href.indexOf('?');
        var search = window.location.href.substr(position);

        if(!search) return null;
        
        // zoo=xxx&foo=bar
        search = search.substring(1);
        
        var params = search.split('&');
        var ret = '';
        var tmp = null;
        
        for(var i=0,len=params.length; i<len; i++) {
            tmp = params[i].split('=');
            if(name === tmp[0]) {
                ret = decodeURIComponent(tmp[1]);
                break;
            }
        }
        
        return ret;
    }

1.注意,这个location.search在使用的时候如果是React等框架,这样url表现为http://localhost:3000/#/homepage/hotwbs 格式,

这样/#后面部分会被认为是锚点,所以location.search获取不到,所有变通为下面两行代码

posted on 2017-06-26 20:45  taoshengyijiuai  阅读(182)  评论(0编辑  收藏  举报