Last active
March 24, 2025 08:43
-
-
Save edjw/5e3725693497dba912d591009c669aec to your computer and use it in GitHub Desktop.
getParams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets URL query parameters from the current location | |
* @returns {Object} An object containing all query parameters as key-value pairs | |
* @example | |
* // URL: https://example.com?name=John&age=25 | |
* const params = getParams(); | |
* // Result: { name: "John", age: "25" } | |
* | |
* @example | |
* // With destructuring | |
* // URL: https://example.com?name=John&age=25&country=UK | |
* const { name, country } = getParams(); | |
* console.log(`${name} is from ${country}`); // "John is from UK" | |
*/ | |
function getParams() { | |
try { | |
const queryString = window.location.search; | |
const urlParams = new URLSearchParams(queryString); | |
const params = {}; | |
for (const [key, value] of urlParams.entries()) { | |
params[key] = value; | |
} | |
return params; | |
} catch (error) { | |
console.error("Error parsing URL parameters:", error); | |
return {}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/edjw/5e3725693497dba912d591009c669aec | |
function getParams(){try{const r=window.location.search,e=new URLSearchParams(r),n={};for(const[r,o]of e.entries())n[r]=o;return n}catch(r){return console.error("Error parsing URL parameters:",r),{}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment