Created
June 26, 2012 18:24
-
-
Save RohitRox/2997731 to your computer and use it in GitHub Desktop.
Read url params with javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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