Skip to content

Returns a non-empty object when the query parameters are empty #2

Open

Description

@kikito

A couple tests:

console.log(window.getQueryParameters(""));
console.log(window.getQueryParameters("?"));

This returns something like {"": undefined}.

Activity

  1. kikito commented on Aug 19, 2015

    @kikito
    Author

    Here is my not-one-liner fix:

    window.getQueryParameters = function(str) {
      var param_str = (str || document.location.search).replace(/(^\?)/,'');
      if(!param_str) { return {}; }
      return param_str.split("&").reduce(function(o,n){n=n.split('=');o[n[0]]=n[1];return o},{});
    }
    
  2. added a commit that references this issue on Aug 19, 2015
    7bd791d
  3. kikito commented on Aug 19, 2015

    @kikito
    Author

    Sent a pr with a one-liner which is compatible with IE > 8 only. I also updated my not-one-line fix so that it is proper js (was missing var and return)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions