Skip to content

Instantly share code, notes, and snippets.

@RohitRox
Created June 26, 2012 18:24
Show Gist options
  • Save RohitRox/2997731 to your computer and use it in GitHub Desktop.
Save RohitRox/2997731 to your computer and use it in GitHub Desktop.
Read url params with javascript
// one way
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
//usage
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
// second way
function getURLParameter(name) {
return decodeURIComponent(
(location.search.match(RegExp("[?|&]"+name+'=(.+?)(&|$)'))||[,null])[1]
);
}
//usage
alert(getURLParameter('id'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment