0

I have a URL like below.

something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false

I want to replace the value of parameter showHiddenElements to some new value.

for e.g. exising value in URL -> showHiddenElements=false

I want to change it through JavaScript to -> showHiddenElements=true

Please advise.

Edit: showHiddenElements may not always be false. And In some cases it may not be available.

5
  • developer.mozilla.org/en-US/docs/Web/API/… Commented Feb 14, 2018 at 15:45
  • Can you just use String replace method ? Commented Feb 14, 2018 at 15:47
  • showHiddenElements may not always be false. Or it may not be available at all Commented Feb 14, 2018 at 15:49
  • What values can it be? If it's not available do we add it with the value true? If it's always going to be true why can't you just ignore it and assume it's true? Commented Feb 14, 2018 at 15:51
  • it can be true or false. If its not available, we just need to add a new one as true. Commented Feb 14, 2018 at 15:52

5 Answers 5

2

Use the URL Object:

const url = new URL('http://something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false');
url.searchParams.delete('showHiddenElements');
url.searchParams.append('showHiddenElements', true);

So you just delete the parameter and update it with the new one (not the most elegant) Docs here: https://developer.mozilla.org/fr/docs/Web/API/URL

Sign up to request clarification or add additional context in comments.

Comments

1

You could use String.replace for that:

var url = 'something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false';

newUrl = url.replace('showHiddenElements=false', 'showHiddenElements=true');

You could also do it fancy and use regex:

var url = 'something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false';

newUrl = url.replace(/showHiddenElements=false$/, 'showHiddenElements=true');

The regex would only match showHiddenElements=false if it's on the end of the URL

To see if it's available you could use regex too:

var url = 'something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false';

// If the url doesn't have a showHiddenElements=__any_word__
if (!url.match(/showHiddenElements=\w+/)) {
    url = url + 'showHiddenElements=false';
}

2 Comments

showHiddenElements may not always be false. Please see my edited question.
Edited the answer to include a if
0

var url = "something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false";
alert("Before: "+url);
url = url.replace("&showHiddenElements=false","&showHiddenElements=true");
alert("After: "+url);
//Console.log clips the end so we can't see the result :(

Comments

0

Maybe something liket this:

var loc = window.location.href;
var newLoc = loc.Replace('showHiddenElements=true', 'showHiddenElements=false')

1 Comment

1. It's lowercase and 2. The arguments are reversed
0

A JavaScript Regular Expression should help if you are just treating the URL as a string.

var str = 'something.com/TaskHandler/search.do?action=search&category=basic&page=1&sortBy=NAME&Ascending=true&showHiddenElements=false';

var res = str.replace(/showHiddenElements/i, 'true');

console.log(res);

Comments

Your Answer

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.