Created
July 29, 2020 20:37
-
-
Save drhisham-code/e92d4c49512877b4860f58b42c58a41b to your computer and use it in GitHub Desktop.
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
//Assuming that our URL is https://example.com/?product=shirt&color=blue&newuser&size=m, | |
//we can grab the query string using window.location.search: | |
const queryString = window.location.search; | |
// console.log(queryString); | |
// ?product=shirt&color=blue&newuser&size=m | |
//We can then parse the query string’s parameters using URLSearchParams: | |
const urlParams = new URLSearchParams(queryString); | |
//Then we call any of its methods on the result. | |
//For example, URLSearchParams.get() will return the first value associated with the given search parameter: | |
const product = urlParams.get('product') | |
console.log(product); | |
// shirt | |
const color = urlParams.get('color') | |
console.log(color); | |
// blue | |
const newUser = urlParams.get('newuser') | |
console.log(newUser); | |
// empty string | |
Checking for the Presence of a Parameter | |
You can use URLSearchParams.has() to check whether a certain parameter exists: | |
console.log(urlParams.has('product')); | |
// true | |
console.log(urlParams.has('paymentmethod')); | |
// false | |
Getting All of a Parameter’s Values | |
You can use URLSearchParams.getAll() to return all of the values associated with a particular parameter: | |
console.log(urlParams.getAll('size')); | |
// [ 'm' ] | |
//Programmatically add a second size parameter. | |
urlParams.append('size', 'xl'); | |
console.log(urlParams.getAll('size')); | |
// [ 'm', 'xl' ] | |
const | |
keys = urlParams.keys(), | |
values = urlParams.values(), | |
entries = urlParams.entries(); | |
for (const key of keys) console.log(key); | |
// product, color, newuser, size | |
for (const value of values) console.log(value); | |
// shirt, blue, , m | |
for(const entry of entries) { | |
console.log(`${entry[0]}: ${entry[1]}`); | |
} | |
// product: shirt | |
// color: blue | |
// newuser: | |
// size: m | |
function getAllUrlParams(url) { | |
// get query string from url (optional) or window | |
var queryString = url ? url.split('?')[1] : window.location.search.slice(1); | |
// we'll store the parameters here | |
var obj = {}; | |
// if query string exists | |
if (queryString) { | |
// stuff after # is not part of query string, so get rid of it | |
queryString = queryString.split('#')[0]; | |
// split our query string into its component parts | |
var arr = queryString.split('&'); | |
for (var i = 0; i < arr.length; i++) { | |
// separate the keys and the values | |
var a = arr[i].split('='); | |
// set parameter name and value (use 'true' if empty) | |
var paramName = a[0]; | |
var paramValue = typeof (a[1]) === 'undefined' ? true : a[1]; | |
// (optional) keep case consistent | |
paramName = paramName.toLowerCase(); | |
if (typeof paramValue === 'string') paramValue = paramValue.toLowerCase(); | |
// if the paramName ends with square brackets, e.g. colors[] or colors[2] | |
if (paramName.match(/\[(\d+)?\]$/)) { | |
// create key if it doesn't exist | |
var key = paramName.replace(/\[(\d+)?\]/, ''); | |
if (!obj[key]) obj[key] = []; | |
// if it's an indexed array e.g. colors[2] | |
if (paramName.match(/\[\d+\]$/)) { | |
// get the index value and add the entry at the appropriate position | |
var index = /\[(\d+)\]/.exec(paramName)[1]; | |
obj[key][index] = paramValue; | |
} else { | |
// otherwise add the value to the end of the array | |
obj[key].push(paramValue); | |
} | |
} else { | |
// we're dealing with a string | |
if (!obj[paramName]) { | |
// if it doesn't exist, create property | |
obj[paramName] = paramValue; | |
} else if (obj[paramName] && typeof obj[paramName] === 'string'){ | |
// if property does exist and it's a string, convert it to an array | |
obj[paramName] = [obj[paramName]]; | |
obj[paramName].push(paramValue); | |
} else { | |
// otherwise add the property | |
obj[paramName].push(paramValue); | |
} | |
} | |
} | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment