Getting URL Query Values
14 March 2019
This was a fun little task. I needed to get URL query values. This is a pretty common requirement, so I thought I would document it’s use here. The easiest way to do this is by creating an instance of URLSearchParams.
Click the button to create a token and reload the page.
Search Params
let search = new URLQueryParams(window.location.search);
let output = document.getElementById("output");
let html = "";
search.forEach((value, key) => {
html += `<li><strong>${key}:</strong> ${value}</li>`;
});
output.innerHTML = html;
You can get a single value from the query with the get()
function.
let token = search.get("token");
The complete code for this page is available in Github. Happy coding!