ayuLiao
7/2/2018 - 2:52 AM

js获取url中的值

JavaScript获取url中的值

var getQueryString = function (name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var search;
        if (url === undefined) {
            url = decodeURI(location.href);
        }
        var index = url.indexOf("?");
        search = url.substring(index);
        var r = search.substr(1).match(reg);
        if (r != null)return unescape(r[2]);
        return null;
    };
    
// www.ayuliao.com?name=ayuliao&age=28
var name = getQueryString("name");  // 'ayuliao' 都是str类型
var age = getQueryString("age"); // '28'