admin管理员组

文章数量:1023213

I have a URL like below.

something/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.

I have a URL like below.

something./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.

Share Improve this question edited Feb 14, 2018 at 15:50 Arshad Rehmani asked Feb 14, 2018 at 15:42 Arshad RehmaniArshad Rehmani 2,1855 gold badges26 silver badges45 bronze badges 5
  • developer.mozilla/en-US/docs/Web/API/… – Hazonko Commented Feb 14, 2018 at 15:45
  • Can you just use String replace method ? – Thomas B. Lze Commented Feb 14, 2018 at 15:47
  • showHiddenElements may not always be false. Or it may not be available at all – Arshad Rehmani 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? – Feathercrown 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. – Arshad Rehmani Commented Feb 14, 2018 at 15:52
Add a ment  | 

5 Answers 5

Reset to default 2

Use the URL Object:

const url = new URL('http://something./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/fr/docs/Web/API/URL

You could use String.replace for that:

var url = 'something./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./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./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';
}

var url = "something./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 :(

Maybe something liket this:

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

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

var str = 'something./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);

I have a URL like below.

something/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.

I have a URL like below.

something./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.

Share Improve this question edited Feb 14, 2018 at 15:50 Arshad Rehmani asked Feb 14, 2018 at 15:42 Arshad RehmaniArshad Rehmani 2,1855 gold badges26 silver badges45 bronze badges 5
  • developer.mozilla/en-US/docs/Web/API/… – Hazonko Commented Feb 14, 2018 at 15:45
  • Can you just use String replace method ? – Thomas B. Lze Commented Feb 14, 2018 at 15:47
  • showHiddenElements may not always be false. Or it may not be available at all – Arshad Rehmani 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? – Feathercrown 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. – Arshad Rehmani Commented Feb 14, 2018 at 15:52
Add a ment  | 

5 Answers 5

Reset to default 2

Use the URL Object:

const url = new URL('http://something./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/fr/docs/Web/API/URL

You could use String.replace for that:

var url = 'something./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./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./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';
}

var url = "something./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 :(

Maybe something liket this:

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

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

var str = 'something./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);

本文标签: javascriptReplace the url parameter value using jsStack Overflow