Created
April 8, 2015 07:30
-
-
Save bbrodriges/586534adb7aae53b9dc1 to your computer and use it in GitHub Desktop.
Ввернуть параметры в GET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
function insertParams(params) { | |
var url_params = window.location.search.substring(1).split('&'); | |
var url_params_obj = {}; | |
// пересобираем GET параметры в объект | |
for (var p = 0; p < url_params.length; p++) { | |
var url_param = url_params[p].split('='); | |
url_params_obj[url_param[0]] = url_param[1]; | |
} | |
// мержим с пришедшим | |
url_params_obj = $.extend({}, url_params_obj, params); | |
// чистим | |
Object.keys(url_params_obj).forEach(function(k) { | |
if (!url_params_obj[k]) { | |
delete url_params_obj[k]; | |
} | |
}); | |
// делаем строку | |
var url_params_str = $.param(url_params_obj, true); | |
// редиректим | |
document.location.search = url_params_str; | |
} | |
$('.my_link').on('click', function(e) { | |
insertParams({ | |
'field': 'id', | |
'order': 'desc' | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment