Parse URL and Append Param using Javascript

Sample

$(document).ready(function(){
    $('.locale-switch').on('click', function(){
        var $this = $(this);
        var locale = $this.data('locale');

        var url = append_param(window.location.href, 'locale', locale);
        window.location.href = url;
    });
});


function parse_url(url){
    url = url.replace(/\#/, '');

    var pattern = /(.+)\?(.*)/;
    var result = url.match(pattern);
    //console.log(result);
    if(result == null){
        var base_url = url;
        var search = '';
    }else{
        var base_url = result[1];
        var search = result[2];
    }

    var params = {};
    if(search != ''){
        var groups = search.split('&');
        for(var i = 0; i < groups.length; i++){
            var group = groups[i].split('=');
            params[group[0]] = group[1];
        }
    }
    
    var data = {
        base: base_url, 
        params: params
    }

    return data;
}

function append_param(url, param_name, param_value){
    var url_data = parse_url(url);
    var params = url_data.params;
    params[param_name] = param_value;
    var url = url_data.base + '?' + $.param(params);
    return url;
}