This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have an unknown number of params in the URL. I want to write those in an Array, which i can use to refin a search.
But i don't know how. I found a big amount of scripts that work with just one Parameter. The idea is to work with for each, or something like this.
Example Url:
https://exampleurl.com&func=ll&nexturl=&promting=done&inputLabel1=(OTSubType:0 or OTSubType:144)&DSmaxrows=20&&folds=checked
Thanks for your help!
Greetings, gefler
function getParams(url){
var params = [];
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
params.push({ key: key, value: value });
});
return params;
}
var yourUrl = "https://exampleurl.com&func=ll&nexturl=&promting=done&inputLabel1=(OTSubType:0 or OTSubType:144)&DSmaxrows=20&&folds=checked";
console.log(getParams(yourUrl))
Related
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&
i have a string like this and i trying to get specific value from it, it is in text format. what can i do if i want to get value of 'aa' it null and value of 'bb' it is 0108135719 tats it. I'm tried different regex but unable to get the desired output.
You can do something like
var t="aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&".split("&")
console.log(t)
var ansObj = {}
t.forEach((element) => {
const elementArray = element.split("=")
const key = elementArray[0]
const value = elementArray[1]
ansObj[key] = value
})
console.log(ansObj)
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'm trying to filter some JSON data, but I would like the lookup to be based on selection of a drop down. However, I just can't get the syntax correct when trying to do this. Currently the following works in my code, great:
var as = $(json).filter(function (i, n) {
return (n.FIELD1 === "Yes"
});
However, what I would like to do is replace the FIELD1 value with a var from the drop down. Something like this following, which is not working:
var dropdownResult = "FIELD1";
var as = $(json).filter(function(i, n) {
return (n.dropdownResult === "Yes"
});
I'm trying to get the var to become the field name after the n. but it's not working.
Thanks for your time. Sorry if this has been answered many times before and is obvious to you.
To use a variable value as the key of an object you should use bracket notation, like this:
var dropdownResult = "FIELD1";
var as = $(json).filter(function(i, n) {
return n[dropdownResult] === "Yes";
});
I removed the extraneous ( you left in your code - I presume this was just a typo as it would have created a syntax error and stopped your code from working at all.
Also note that it's much better practice to use a boolean value over a string 'Yes'/'No'
This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 6 years ago.
i have a url like this
test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14
i want to remove the parameter using the following javascript
function removeParam(uri) {
uri = uri.replace(/([&\?]start_date=*$|start_date=*&|[?&]start_date=(?=#))/, '');
return uri.replace(/([&\?]end_date=*$|end_date=*&|[?&]end_date=(?=#))/, '');
}
but it didn't work, anyone know what's wrong with that?
in modern browsers you can do this quite simply
var x = new URL(location.origin + '/test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14');
x.searchParams.delete('start_date');
x.searchParams.delete('end_date');
var uri = x.pathname.substr(1) + x.search; // substr(1) because you don't have a leading / in your original uri
at least, I think it's simpler
Your RegExp is no match!
If you want remove end_date, you should:
uri.replace(/(end_date=)([^*&]+)/, 'end_date=')
And so on.
This question already has answers here:
Remove querystring from URL
(11 answers)
Closed 7 years ago.
How would I remove part of a URL with jQuery, when the said part is changeable?
E.g. how can I remove this from my URL: ?modal=name
when 'name' might be name1 name2 or name3
I guess I will need to split the URL, but how do I do this when the modal name might be anything?
var url = 'http://yourdomain.com/search?modal=name';
alert(url.substring(0, url.indexOf('?')));
Demo
As you have asked "How to remove the part of a URL" using jQuery
Here is a basic workaround for you:
//the URL ( decode it just for GOOD practice)
var someRandomUrl = decodeURI("http://example.com?modal=name");
//here we do the splitting
var splittedParts = someRandomUrl.split("?");
// the first part of the array will be URL you will require
var theURL = splittedParts[0];
// the second part of the array will be the Query String
var theQuery = splittedParts[1];
alert(theURL);
This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found