I am trying to remove the url param after function was completed, so it won't break the uix.
Here how it looks if param is not removed
http://localhost:1337/check?domain=fb.com&success=task&success=note .
I need it to look like this http://localhost:1337/check?domain=fb.com&success=task after every call.
Will be glad for help
Get the URL search query string by window.location.search and parse it with URLSearchParams.
Use getAll(name) to get all values of the parameter as an array.
Remove the desired element from the array.
Edit it back in with set(name, value)
Then use window.location to change the URL with toString()'s result (back as a string).
const searchParams = new URLSearchParams(window.location.search);
const success = searchParams.getAll('success')
success.pop(); // ['task']
searchParams.set('success', success);
window.location.search = searchParams.toString();
Related
const url = new URLSearchParams('https://example.com?q1=1&q2=2');
console.log(url.has('q3')) // returns false as expected
console.log(url.has('q2')) // returns true as expected
console.log(url.has('q1')) // returns false as NOT expected
Why it happens?
The URLSearchParams constructor, if passed a string, expects that string to be a query string and not a complete URL.
q1 doesn't appear because your first parameter is https://example.com?q1.
const url = new URLSearchParams('https://example.com?q1=1&q2=2');
console.log([...url.entries()]);
Use the URL constructor if you want to parse a complete URL.
const url = new URL('https://example.com?q1=1&q2=2');
console.log(url.searchParams.has('q3'))
console.log(url.searchParams.has('q2'))
console.log(url.searchParams.has('q1'))
I'm trying to get the parameter used in URL with java script.
example URL:
`http://localhost:8002/all-products?categories=Events%20&%20Festivals`
I use this code to get the value in categories.
const urlParams = new URLSearchParams(queryString);
const category_name = urlParams.get("categories")`
I only get Events as an output i need to get Events & Festivals but i cant seems to get it
you can add some other character instead of &
for eg. '-'
`http://localhost:8002/all-products?categories=Events-Festivals`
OR
you can encrypt the category value and pass it in URL
There are hundreds of example out here showing how to get URL parameters but I couldn't find any to list ONLY parameters (variables) name and not values?
For example, if I have something like:
www.mydomain.com/filtres?makes=honda,mazda,jeep&models=crv,cx5,wamgler&years=2008
I want to get only makes,models,years and if URL change to
www.mydomain.com/filtres?makes=honda,mazda,jeep&models=crv,cx5,wamgler
should be like makes,models
or on
www.mydomain.com/filtres?makes=honda,mazda,jeep&models=crv,cx5,wamgler&years=2008,2010&city=vancouver
the out should be like makes,models,years,city
You can use URLSearchParams
Sample:
const queryString = "?makes=honda,mazda,jeep&models=crv,cx5,wamgler&years=2008"
const urlParams = new URLSearchParams(queryString);
console.log([...new URLSearchParams(urlParams).keys()]);
console.log(
[...new URLSearchParams(window.location.search).keys()].join(',')
);
I have a from with inputs with array name like foo[] so when submitting the form my url become foo[]=lorem&foo[]=ipsum but when using php http_build_query to regenerate this sort of query it adds key to the array like this foo[0]=lorem&foo[1]=ipsum.
My issue is that on the front I'm using URLSearchParams.getAll('foo[]') to get the value, but it works only without the array keys added by http_build_query.
So is there a way to use URLSearchParams with this format ?
Based on #Plamen Nikolov link I fixed my issue by changing my Javascript to handle the inputs that are arrays with or without index keys (added by PHP with http_build_query) like this :
// Before
let urlParams = new URLSearchParams(window.location.search);
// After
let urlQuery = decodeURI(window.location.search).replace(/\[\d+\]/g, "[]");
let urlParams = new URLSearchParams(urlQuery);
I'm looking for a way to get the query string part of a url using node.js or one of it's extension module.
I've tried using url and express but they give me an array of parameters.
I want to original string.
Any ideas ?
example; for:
http://www.mydom.com?a=337&b=33
give me
a=337&b=33
(with or without the ?)
Use url.parse. By default it will return an object whose query property is the query string. (You can pass true as the second argument if you want query to be an object instead, but since you want a string the default is what you want.)
var url = require('url');
var urlToParse = 'http://www.mydom.com/?a=337&b=33';
var urlObj = url.parse(urlToParse);
console.log(urlObj.query);
// => a=337&b=33
How about using the built-in url.parse method?