CODE CODIFY.CLUB

Get query string values in JavaScript

You don't need jQuery for that purpose. You can use just some pure JavaScript: 
 
function getParameterByName(name, url) {
if (!url) {
url
= window.location.href;
}
name
= name.replace(/[[]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results
= regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/+/g, " "));
}
 
 
Usage:
// query string: ?foo=lorem&bar=&baz
var foo = getParameterByName('foo'); // "lorem"
var bar = getParameterByName('bar'); // "" (present with empty value)
var baz = getParameterByName('baz'); // "" (present with no value) 
var baz = getParameterByName('viral'); // "" (present with no value)
var qux = getParameterByName('qux'); // null (absent)  

V!ral

Developer, Technology aficionado, Beer Lover and founder of codify.club Network. Who shares a keen interest about website development, health issues, cancer awareness and other fun stuff.

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most popular

Most discussed