Changing URL parameters with jQuery

You can find plenty of resources about this topic just by googling the web, most of which will point to jQuery plugins.
But the fact is that it’s so easy to achieve this by simply using jQuery that you do not need a plugin.

The code is pretty much self explanatory:

 * queryParameters -> handles the query string parameters
 * queryString -> the query string without the fist '?' character
 * re -> the regular expression
 * m -> holds the string matching the regular expression
var queryParameters = {}, queryString =,
    re = /([^&=]+)=([^&]*)/g, m;

// Creates a map with the query string parameters
while (m = re.exec(queryString)) {
    queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);

// Add new parameters or update existing ones
queryParameters['newParameter'] = 'new parameter';
queryParameters['existingParameter'] = 'new value';

 * Replace the query portion of the URL.
 * jQuery.param() -> create a serialized representation of an array or
 *     object, suitable for use in a URL query string or Ajax request.
 */ = $.param(queryParameters); // Causes page to reload

You can clearly improve the regular expression, but the one above meet my needs.

Published by

Samuel Santos

Java developer, Open Source hacker, Web technologist, JUG Leader.

  • Picky Web Design

    Thanks for such a nice post. I was just looking for one of my website and it works ..

  • João Cunha

    Awesome script. It works exactly the way I wanted to with no need to use bloated jQuery plugins.

    Thanks a lot,

  • Hugo

    Awesome! Clean and simple. Thanks

  • Jan Zitniak


  • tomca

    right on – thanks for sharing! In a world of plugin links, this one stands out!

  • Sindre Sorhus

    Using regex for this is just wrong and will fail in some scenarios.

    You’d better off using a tiny reusable module like:

    • Samuel Santos

      Nice module. Thanks for pointing it out!

  • http://malparty.Fr Xavv

    Whooow !! Finally a well and not hardcoded perfect method ! Thanks a lot !! 😉

  • Carlos Júlio

    Nice post. Thanks

  • ramswaroop

    Hey, thanks for the code snippet but I am having trouble updating a number as a value in one of the query parameter.

  • Wardak

    Simply brilliant and excellent; exactly what I was looking for