How to get GET parameter values from URL in JavaScript?
Hi everyone! I'm a beginner in JavaScript and I've encountered this task: I have a page that opens with GET parameters in the URL, for example:\n\n```\nhttps://example.com/page?name=John&age=25&city=New%20York\n```\n\nI need to extract the values of these parameters (name, age, city) and use them in the code. I tried using `window.location.search`, but it only returns the string `?name=John&age=25&city=New%20York`. I don't know how to parse it further. I attempted to write my own function using `split('&')`, but I'm confused about decoding characters (e.g., `%20` instead of a space) and handling special characters. There's also the issue if a parameter is repeated (e.g., `?id=1&id=2`). Please advise on how to correctly and reliably get all parameters as an object? Preferably without jQuery, just plain JavaScript. Thanks.
No answers yet.
Log in to leave an answer
Blogs