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.
- 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 befalse
. 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
orfalse
. If its not available, we just need to add a new one astrue
. – Arshad Rehmani Commented Feb 14, 2018 at 15:52
5 Answers
Reset to default 2Use 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.
- 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 befalse
. 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
orfalse
. If its not available, we just need to add a new one astrue
. – Arshad Rehmani Commented Feb 14, 2018 at 15:52
5 Answers
Reset to default 2Use 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
版权声明:本文标题:javascript - Replace the url parameter value using js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745556677a2155918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论