Skip to content

Instantly share code, notes, and snippets.

@townivan
Created July 29, 2014 13:02
Show Gist options
  • Save townivan/22818255d0a99843bb08 to your computer and use it in GitHub Desktop.
Save townivan/22818255d0a99843bb08 to your computer and use it in GitHub Desktop.
Quick querystring with javascript example
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jquery for querystring parsing</title>
</head>
<body>
<p>To try this out, add something like <pre>?fname=Joe&lname=Smith</pre> to the end of the URL for this page.</p>
first name: <input id="fnamebox" type="text"><br>
last name: <input id="lnamebox" type="text"><br>
<script>
var fname = getParameterByName('fname');
var lname = getParameterByName('lname');
if (fname != -1){
document.getElementById("fnamebox").value = fname;
}
if (lname != -1){
document.getElementById("lnamebox").value = lname;
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment