Skip to content

Instantly share code, notes, and snippets.

@okerx
Last active April 20, 2021 16:29
Show Gist options
  • Save okerx/330fe1a1563f4916475c54a02e6ea4ab to your computer and use it in GitHub Desktop.
Save okerx/330fe1a1563f4916475c54a02e6ea4ab to your computer and use it in GitHub Desktop.

Revisions

  1. Ammar Oker revised this gist Apr 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion url-params-to-js-object.js
    Original file line number Diff line number Diff line change
    @@ -63,4 +63,4 @@ function getUrlParams(link) {
    return obj;
    }

    console.log(getUrlParams('https://github.com/?foo=bar')); // results: { foo: "bar" }
    console.log(getUrlParams('https://github.com/?foo=bar')); // results: { foo: "bar" }
  2. Ammar Oker revised this gist Apr 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion url-params-to-js-object.js
    Original file line number Diff line number Diff line change
    @@ -63,4 +63,4 @@ function getUrlParams(link) {
    return obj;
    }

    var params = getUrlParams();
    console.log(getUrlParams('https://github.com/?foo=bar')); // results: { foo: "bar" }
  3. Ammar Oker renamed this gist Apr 20, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Ammar Oker renamed this gist Apr 20, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Ammar Oker created this gist Apr 20, 2021.
    66 changes: 66 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    function getUrlParams(link) {

    // get query string from url (optional) or window
    var queryString = link ? link.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;
    }

    var params = getUrlParams();