Most Optimal Way To Get Query Param in Javascript

I’m working with Javascript only here because this is an advertising lander (landing page) and the client doesn’t want jQuery slowing down web pages. I need some Javascript that is the most optimal way (least lines of code) to get the ‘tid’ query parameter on the lander. For instance, imagine this advertisement link that goes to a lander:

Can you provide the most optimal way to get query parameters in Javascript, and make it work even from IE7 and forward (FF, Chrome, Safari, Opera, and IE)? Here’s what I’ve come up with, but perhaps you can optimize it even tighter? The reason for getting this even tighter is so that the snippet for the pixel tracking is as small as possible. I mean, I’m already having extra lines (not shown here) to create an array of postback parameters, and then calling a remote Javascript to pass these extra parameters to the PIWIK tracking system.

try {
  var q = {};
  var ls1 = + '&';
  var ls2 = ls1.split('&');
  for(var i1 in ls2) {
    var i2 = ls2[i1];
    q[i2.split('=')[0]] = i2.split('=')[1];
} catch(e) {}

// I can then use q.tid to get the ‘tid’ query parameter on the URL.

Source: cross-browser

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.