Skip to content

Instantly share code, notes, and snippets.

@kartick14
Created April 15, 2021 11:28
Show Gist options
  • Save kartick14/cccf88766c1a92459200af7b7006b16d to your computer and use it in GitHub Desktop.
Save kartick14/cccf88766c1a92459200af7b7006b16d to your computer and use it in GitHub Desktop.
Jquery get current url and add/push parameter
function dropdownChange(){
var byDate = document.getElementById('filter-by-date').value;
var myUrl = addQSParm('cat_type',cat_type);
var myUrl = addQSParm('keyword',sKeyword, myUrl);
window.location.href = myUrl;
}
/**************************************************
@ Constructing a URL with parameters using jQuery
***************************************************/
function addQSParm(name, value, myUrl = '') {
if(myUrl == ''){
var myUrl = window.location.origin+''+window.location.pathname;
}
var re = new RegExp("([?&]" + name + "=)[^&]+", "");
function add(sep) {
myUrl += sep + name + "=" + encodeURIComponent(value);
}
function change() {
myUrl = myUrl.replace(re, "$1" + encodeURIComponent(value));
}
if (myUrl.indexOf("?") === -1) {
add("?");
} else {
if (re.test(myUrl)) {
change();
} else {
add("&");
}
}
return myUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment