0

I have a URL like this

http://localhost:60041/AnnualAppraisal/PotentialQuotient.aspx?EmpCode=677593&FYDetailID=1045&FYID=36&FYName=2017-18&Grade=F2&EmpReviewTypeId=73&FEnabled=70&PageType=MGR

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.

asked Jan 29, 2018 at 12:09

2 Answers 2

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);

answered Jan 29, 2018 at 12:33
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I wanted. Thanks for the help :) :)
Glad I could help. :)
1

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

answered Jan 29, 2018 at 12:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.