Skip to content

Instantly share code, notes, and snippets.

@kklai
Created February 10, 2016 20:12
Show Gist options
  • Save kklai/75951736d4109d36637d to your computer and use it in GitHub Desktop.
Save kklai/75951736d4109d36637d to your computer and use it in GitHub Desktop.
<html>
<head>
<title></title>
<style type="text/css">
body {
padding: 25%;
}
</style>
</head>
<body>
<input id="hello">
<button id="button">SUBMIT</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript">
///////////////// part 1 /////////////////
// function to add parameter to url
function insertParam(key, value) {
key = encodeURI(key); value = encodeURI(value);
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
//this will reload the page, it's likely better to store this until finished
document.location.search = kvp.join('&');
}
// click button and add user input to url
$("#button").on("click", function(d){
var response = $("#hello").val();
insertParam("userResponse", response);
})
///////////////// part 2 /////////////////
// detect parameter on load
var userResponse = location.search.split('userResponse=')[1]
if (userResponse) {
$("body").append("<h1>" + userResponse +"</h1>")
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment