I have a URL like this
I have a save button and I save the data in db through ajax call.
After save I changed the FEnabled to 71 in my database and how can I change the query string parameter in the URL, as I do only ajax post and not redirecting to any page.
I see a lot of answer in sw but they only have one query string parameter.
Please help me. Thanks in advance.
2 Answers 2
You can use regex /(?<=FEnabled=)(.*)(?=\&)/ with .match() and .replace().
var url = 'http://localhost:60041/AnnualAppraisal/PotentialQuotient.aspx?EmpCode=677593&FYDetailID=1045&FYID=36&FYName=2017-18&Grade=F2&EmpReviewTypeId=73&FEnabled=70&PageType=MGR';
var regex = /(?<=FEnabled=)(.*)(?=\&)/;
var number = parseInt(url.match(regex)[0]);
console.log('This is the old number: ' + number);
var url = url.replace(regex, number+1);
console.log('This is the new url: ' + url);
2 Comments
Try using this function I did long time ago in javascript. It should work:
function replaceURLParameter(key, value) {
// value = value.split(' ').join('-'); //replaces spaces with dashes
var parameter = key + "=" + value;
var url = window.location.href;
var urlparts = url.split('?');
var finalUrl = false;
if (urlparts.length >= 2) {
/*url has parameters*/
var pars = urlparts[1].split(/[&;]/g);
var exists = false;
pars.forEach(function (element, index) {
var k = element.split('=')[0];
var v = element.split('=')[1];
if (k == key) {
exists = true;
if (!value)
pars.splice(index, 1);
else
pars[index] = parameter;
}
});
if (exists) {
finalUrl = urlparts[0] + "?";
pars.forEach(function (elem, index) {
finalUrl += elem;
if (index != pars.length)
finalUrl += "&";
});
}
if (!exists) {
/* The parameter to add doesnt exists but we have others. */
finalUrl = url + '&' + parameter;
finalUrl = setPage(1, finalUrl);
}
}
if (urlparts.length < 2) {
/*url without parameters*/
finalUrl = url + "?" + parameter;
}
return finalUrl;
}
the call in your example should be replaceURLParameter('FEnabled',71) and it should return the full url (string) with the new parameter in the query string