Created
April 15, 2021 11:28
-
-
Save kartick14/cccf88766c1a92459200af7b7006b16d to your computer and use it in GitHub Desktop.
Jquery get current url and add/push parameter
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 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