본문 바로가기

Develop/HTML & JavaScript & CSS

[URL] 파라미터 추가 삭제 url parameters add/remove

출처 - https://motolies.tistory.com/681

function urlRemoveParam(url, key) {
    var rtn = url.split("?")[0],
        param,
        params_arr = [],
        queryString = (url.indexOf("?") !== -1) ? url.split("?")[1] : "";
    if (queryString !== "") {
        params_arr = queryString.split("&");
        for (var i = params_arr.length - 1; i >= 0; i -= 1) {
            param = params_arr[i].split("=")[0];
            if (param === key) {
                params_arr.splice(i, 1);
            }
        }
        if(params_arr.length > 0)
            rtn = rtn + "?" + params_arr.join("&");
    }
    return rtn;
}

function urlAddParam(url, key, value){
    return url + ( url.match( /[\?]/g ) ? '&' : '?' ) + key + "=" + value;
}

 

참고 : https://developer-talk.tistory.com/815