Parse URL parameters using JavaScript

I recently found myself in a situation where I had very limited access to remote content and needed to perform specific actions conditionally. I wanted a quick and clean way to parse URL query parameters inline.

var vars = {};^\??|&)([^&=]*)=?([^&]*)/g, 
  function(i, j, k) { if (j) vars[j] = k; });

Given the URL:

The associated array vars will be populated with keys vars.a and vars.b with the string values defined in the URL query parameters.

This method can be easily modified to parse the hash portion of the URL, if needed.

Comments Off on Parse URL parameters using JavaScript

Categories random