Skip to content

Instantly share code, notes, and snippets.

@nanusdad
Created December 8, 2016 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanusdad/4264b5a99d993bfe503d95c3248d4570 to your computer and use it in GitHub Desktop.
Save nanusdad/4264b5a99d993bfe503d95c3248d4570 to your computer and use it in GitHub Desktop.
HTML / JS code to read and process URL query details
<html>
<head>
<title>Confirmation Form</title>
</head>
<body>
<p id="demo">
</p>
<script>
// Get URL parameters
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
console.log(results);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Define Field names to read
var fieldsArray = ['Field1', 'Field2', 'Field3'];
// Array to store values
var numArray = [];
// function to retrieve and push values into numArray
function logArrayElements(element, index, array) {
console.log(getParameterByName(element));
numArray.push(parseInt(getParameterByName(element)));
}
// Get values
fieldsArray.forEach(logArrayElements);
// function to get sum total of array elements
function getSum(total, num) {
return total + num;
}
// Generate HTML with total value
document.getElementById("demo").innerHTML = "The total is: " + numArray.reduce(getSum);
</script>
</body>
<html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment