I have three functions to handle between url parameters but its not good enough , i mean its little complicated , and im looking for simple functions to use between all those three .
function getUrlParameter(sParam) { // this function to get single parameter
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
function replaceUrlParam(paramName, paramValue) { // this to replace parameters value by other value
var currentUrl = window.location.href;
var pattern = new RegExp('(' + paramName + '=).*?(&|$)')
var newUrl = currentUrl.replace(pattern, '$1' + paramValue + '$2');
if (newUrl == currentUrl && newUrl.indexOf(paramName + '=' + paramValue) == -1) {
newUrl = newUrl + (newUrl.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue
}
window.history.pushState('', document.title, newUrl);
return newUrl;
}
function getUrlParameters() { // this to get all parameters
var sParam = 'p'; var viewParam = 'v'; var lanPram = 'l'; var ukomPram = 'uk'; var catPram = 'ca'; var ucatPram = 'uc';
l = getUrlParameter(lanPram);
ukmun = getUrlParameter(ukomPram);
kat = getUrlParameter(catPram);
uKat = getUrlParameter(ucatPram);
pagenumber = getUrlParameter(sParam);
viewIndex = getUrlParameter(viewParam);
if (l || l == 0) { l = l; } else { l = replaceUrlParam(lanPram, 0); }
if (ukmun) { ukmun = ukmun; } else { ukmun = replaceUrlParam(ukomPram, 0); }
if (kat) { kat = kat; } else { kat = replaceUrlParam(catPram, 0); }
if (uKat) { uKat = uKat; } else { uKat = replaceUrlParam(ucatPram, 0); }
if (viewIndex) { viewIndex = viewIndex; } else { viewIndex = 'l'; }
if (pagenumber) { pagenumber = pagenumber; } else { pagenumber = 1; }
}
this works but it calls many the function replaceUrlParam
and the function getUrlParameter
And the big thing which i dont like is when the page is loaded with those urls then if i want go back to previous page it goes back by every parameter
ex: if have this url myfile.php?p=1&v=l&l=0&uk=0&ca=0&uc=0
then if i click previous to go previous page in browser i go back by one parameter like that
myfile.php?p=1&v=l&l=0&uk=0&ca=0
then to go previous again it stays in same page with next param like that myfile.php?p=1&v=l&l=0&uk=0
and so on till the parameters are gone .
Is there an easy work around those 3 functions to make it easier and simple ?
Thanks.