Get escaped URL parameter

  1. Home
  2. javascript
  3. Get escaped URL parameter

I’m looking for a jQuery plugin that can get URL parameters, and support this search string without outputting the JavaScript error: “malformed URI sequence”. If there isn’t a jQuery plugin that supports this, I need to know how to modify it to support this.


The value of the URL parameter, when decoded, should be:


(the characters are Norwegian).

I don’t have access to the server, so I can’t modify anything on it.

First answer

You should not use jQuery for something like this!
The modern way is to use small reusable modules through a package-manager like Bower.

I’ve created a tiny module that can parse the query string into an object. Use it like this:

// parse the query string into an object and get the property
//=> æøå

Second answer

What you really want is the jQuery URL Parser plugin. With this plugin, getting the value of a specific URL parameter (for the current URL) looks like this:


If you want an object with parameter names as keys and parameter values as values, you’d just call param() without an argument, like this:


This library also works with other urls, not just the current one:

$('#myElement').url().param(); // works with elements that have 'src', 'href' or 'action' attributes

Since this is an entire URL parsing library, you can also get other information from the URL, like the port specified, or the path, protocol etc:

var url = $.url('');
url.attr('protocol'); // returns 'http'
url.attr('path'); // returns '/folder/dir/index.html'

It has other features as well, check out its homepage for more docs and examples.

Instead of writing your own URI parser for this specific purpose that kinda works in most cases, use an actual URI parser. Depending on the answer, code from other answers can return 'null' instead of null, doesn’t work with empty parameters (?foo=&bar=x), can’t parse and return all parameters at once, repeats the work if you repeatedly query the URL for parameters etc.

Use an actual URI parser, don’t invent your own.

For those averse to jQuery, there’s a version of the plugin that’s pure JS.

Third answer

If you don’t know what the URL parameters will be and want to get an object with the keys and values that are in the parameters, you can use this:

function getParameters() {
  var searchString =,
      params = searchString.split("&"),
      hash = {};

  if (searchString == "") return {};
  for (var i = 0; i < params.length; i++) {
    var val = params[i].split("=");
    hash[unescape(val[0])] = unescape(val[1]);
  return hash;

Calling getParameters() with a url like /posts?date=9/10/11&author=nilbus would return:

  date:   '9/10/11',
  author: 'nilbus'

I won’t include the code here since it’s even farther away from the question, but posted a library that allows manipulation of the parameters in the URL too:

Spread the love

Related articles

Comments are closed.