Created
July 29, 2014 13:02
-
-
Save townivan/22818255d0a99843bb08 to your computer and use it in GitHub Desktop.
Quick querystring with javascript example
This file contains hidden or 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
<!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