How to extract URL query string values using JavaScript

I’m working with a web page that receives data through URL parameters. Here’s an example of what I’m dealing with:

// Example URL: example.com/page.html?name=john&age=25&items=apple-banana-orange-grape
const currentUrl = window.location.href;
console.log(currentUrl);

I’m trying to grab the complete value of the items parameter, but when I attempt to parse it, I only get apple instead of the full string apple-banana-orange-grape. It seems like the dashes are causing issues with my parsing logic. What’s the proper way to retrieve the entire parameter value in JavaScript? I’ve tried a few different approaches but keep running into the same problem where only the first part gets captured.

URLSearchParams is the way to go here, but you can also grab query parameters directly from window.location.search and split manually if needed. URLSearchParams handles URL decoding for you though, so it’s cleaner. I’ve hit this before - usually it’s not the dashes causing problems, it’s how you’re splitting or parsing the query string. Try logging window.location.search first to see what’s actually getting passed. Half the time the issue is how the URL gets built on the client side, not your parsing.

The dashes aren’t your problem here. URLSearchParams handles dashes just fine - new URLSearchParams(window.location.search).get('items') should return the full apple-banana-orange-grape string with no issues. Something’s going wrong in your parsing logic after you grab the parameter. Check if you’re splitting on dashes somewhere in your code or doing some other processing that’s cutting off the value. Log the raw parameter first before you perform any further operations - that’ll help identify if you’re actually getting the complete string from the URL.

dashes shouldnt be a prob. use new URLSearchParams(window.location.search).get('items') to get the full string. maybe there’s an issue in your parsing logic. what method r u using?