javascript - Get escaped URL parameter

ID : 10199

viewed : 21

Tags : javascripturlquery-stringurl-parametersjavascript

Top 5 Answer for javascript - Get escaped URL parameter

vote vote


function getURLParameter(name) {     return decodeURI(         (RegExp(name + '=' + '(.+?)(&|$)').exec(||[,null])[1]     ); } 
vote vote


Below is what I have created from the comments here, as well as fixing bugs not mentioned (such as actually returning null, and not 'null'):

function getURLParameter(name) {     return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(||[,""])[1].replace(/\+/g, '%20'))||null; } 
vote vote


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:

$.url('').param(); $('#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.

vote vote


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:

vote vote


You can use the browser native property:

function getParameter(paramName) {   var searchString =,       i, val, params = searchString.split("&");    for (i=0;i<params.length;i++) {     val = params[i].split("=");     if (val[0] == paramName) {       return unescape(val[1]);     }   }   return null; } 

But there are some jQuery plugins that can help you:

Top 3 video Explaining javascript - Get escaped URL parameter