Javascript

Javascript: Add URL parameter

Here’s a handy function for adding a new parameter to a URL:

function addUrlParam(param, value)
{
   var url = location.href;
   if (location.search.indexOf(param) != -1) return url;
   var hash = location.hash, sep = url.indexOf('?') == -1 ? '?' : '&';
   return url.replace(hash,'') + sep + encodeURIComponent(param) + (value ? '=' + encodeURIComponent(value) More >

Javascript addClass/removeClass functions

While looking for compact Javascript addClass and removeClass functions, I stumbled upon http://snipplr.com/view/3561/addclass-removeclass-hasclass/.

This code allows for a few optimizations:

  • The ‘match’ method is deprecated, ‘test’ should be used instead.
  • The ‘addClass’ function references a non-existing ‘this’ object.
  • The ‘removeClass’ function declares a new variable ‘reg’, which is unnecessary.
  • The ‘removeClass’ function doesn’t handle multiple More >