More often than not we need to change URL parameters sometimes for Hash tags and sometimes for creating url parameters for creating new clickable URLs..
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
The above function simply uses regular expressions, to look for & and ? and query sting concatenated with key we are trying to replace, if the match is found it replaces it else simply concatenates the new parameter..
The above function is taken from the link...
http://stackoverflow.com/questions/5999118/add-or-update-query-string-parameter
0 Comment(s)