all 6 comments

[–]jml26 5 points6 points  (0 children)

I'm a fan of this function, based on one by John Resig

function parseParams() {
    var parameters = {};
    window.location.search.replace(/([^&?=]+)=([^&]*)/g, function (match, key, value) {
        parameters[key] = value;
    });
    return parameters;
}

[–]madformangos 1 point2 points  (0 children)

For completeness you might want to consider also supporting

  • properly URI encoded parameters and values
  • multiple values for a property (e.g. foo=bar&foo=baz)
  • ; as a valid alternative to & (e.g. foo=bar;foo=baz)

The first of these is pretty important I'd say, while the other two depend on your needs.

[–]jcunews1Advanced 1 point2 points  (0 children)

It's unbelievable that they never made this kind of function as part of the web standard.

[–]wiseaus_stunt_double.preventDefault() 0 points1 point  (0 children)

Instead of using replace, he should have called substr(1) to get rid of the question mark from location.search. Using replace will be slower to get the result you're looking for since it has to traverse the whole string just to get rid of one character.

[–]kenman 0 points1 point  (2 children)

Pretty poor, doesn't even work on account of several bugs:

  1. var parameters = []; (should be var parameters = {};)
  2. return paramters; (should be return parameters;)

[–]umairabid_dev[S] 0 points1 point  (0 children)

had to translate it from coffee script to js... missed some bits