Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pntrivedy/9498010 to your computer and use it in GitHub Desktop.
Save pntrivedy/9498010 to your computer and use it in GitHub Desktop.
I was searching for a way in javascript by which I can get url query parameters and found this here : http://jsperf.com/querystring-with-javascript probably the best solution.
<script>
window.getParameterByName = function(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) return "";
else return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
usage:
<script>
var search = window.getParameterByName("q");
var value = window.getParameterByName("value");
var undef = window.getParameterByName("undefinedstring");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment