Created
December 8, 2016 07:02
-
-
Save nanusdad/4264b5a99d993bfe503d95c3248d4570 to your computer and use it in GitHub Desktop.
HTML / JS code to read and process URL query details
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
<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