View on GitHub

Ann Molly Paul

learn -- code -- inspire

Querystring creation using Jquery.param

February 1, 2015

Being a backend engineer, I didn't seem to care too much about best practices when coding in Jquery or any front end technology for that matter

It, in most cases, made my code bulky and error prone. This was easily obvious when I refactored a simple implementation of serializing params to create a queyrstring.

var startParam = 'start=' + start,
    stopParam = 'stop=' + stop,
    dateParam = 'date=' + date,
    params = startParam + '&' + stopParam + '&' + dateParam;

Since this serialization is done by pure string formatting, there can be edge cases where an & or other characters that need to be escaped are passed in the query string. Why bother when there are libraries for this purpose :) I went ahead and refactored it by using jquery.param

var paramsDict = { startParam: start,
                    stopParam: stop,
                    dateParam: date };
var params = $.param( paramsDict );