美文网首页
获取url后面的查询参数

获取url后面的查询参数

作者: zhulichao | 来源:发表于2020-07-24 09:05 被阅读0次
    // 获取url后面指定名称的查询参数
    function getUrlQueryString(name) {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        // window.location.search 从?开始的部分,包括?
        var r = window.location.search.substr(1).match(reg);
        if (r) {
            return unescape(r[2]);// 对通过escape()编码的字符串进行解码
        }
        return null;
    }
    // 获取url的查询参数组成的对象
    function getUrlQueryObj() { 
        //获取url中"?"符后的字串 
        var url = location.search;
        var queryObj = {}; 
        if (url.indexOf("?") > -1) { 
            var str = url.substr(1); 
            strs = str.split("&");
            strs.forEach((item) => {
                queryObj[item.split("=")[0]] = unescape(item.split("=")[1]);
            });
        }
        return queryObj; 
    } 
    

    相关文章

      网友评论

          本文标题:获取url后面的查询参数

          本文链接:https://www.haomeiwen.com/subject/tmfjhktx.html