1

捣鼓一个对QueryString中的Search参数进行设置和设置后整个串的获取的方法

/**
 * @(ΦωΦ)
 */
var locationParam = function (param, value) {
    var locationhash = location.search.substr(1);

    var searchParams = locationhash.split('&');

    var paramsMap = {};

    for(var idx in searchParams){
        var searchParam = searchParams[idx];
        var parts = searchParam.split('=');
        if(parts.length == 2){
            paramsMap[parts[0]] = parts[1];
        }
    }

    if (value == undefined) {
        return paramsMap[param];
    } else {
        paramsMap[param] = value;

        var searchParamString = Object.keys(paramsMap).map(function(key){
            var val = paramsMap[key];
            return key + '=' + val;
        }).join('&');

        return location.pathname + '?' + searchParamString + '#' + location.hash.substr(1);
    }
}

![买买买][1]

小朋友
395 声望11 粉丝

引用和评论

0 条评论