<!DOCTYPE html>
<html>
<head>
<title>OBTENER LOS ARGUMENTOS DE ENTRADA GET HTML</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- this code can be saved in a separate file in a subdir js with a name like yourscript.js -->
<!-- linked with <script src="./js/yourscript.js"></script> -->
<script>
function sendMail(subject, message) {
let link = "mailto:anybody@mail.com" + "?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(message);
//console.log(message);
//console.log("LINK:\r\n"+link);
window.location.href = link;
}
function $_GET() {
//===========================================
//auxiliary functions declared previously
//===========================================
const getURLParameters = url =>
(url.match(/([^?=&]+)(=([^&]*))/g) ||[]).reduce(
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
{}
);
//===============================================
// Main code
//===============================================
//get the arguments
//let array_param = param();
let array_param = getURLParameters(window.location.href); //window.location.search.substring(1) window.location.href
let array_keys =[];
array_keys = Object.keys(array_param);
if ((array_keys.length=== 0) || (array_keys[0]==="")) {
let body = document.getElementsByTagName("body")[0];
let texto = document.createTextNode("There are not arguments to capture in the call to this web page ...");
body.appendChild(texto);
} else {
console.table(array_param);
console.table(array_keys);
let body = document.getElementsByTagName("body")[0];
let texto = document.createTextNode("These fields have been received from a form submit in a web page......");
body.appendChild(texto);
//show the arguments received in a table with 2 columns key:value
// Create the elements <table> <thead> <tbody>
let tabla = document.createElement("table");
//creating the header of the table
let tblHeader = document.createElement("thead");
let fila = document.createElement("tr");
let celda = document.createElement("td");
let textoCelda = document.createTextNode("key");
celda.appendChild(textoCelda);
fila.appendChild(celda);
//
celda = document.createElement("td");
textoCelda = document.createTextNode("valor");
celda.appendChild(textoCelda);
fila.appendChild(celda);
//
tblHeader.appendChild(fila);
tabla.appendChild(tblHeader);
//Create the body of the table
let tblBody = document.createElement("tbody");
// Creating the cells of the table
//loop to read the arguments
for (let i = 0, num_elements = array_keys.length; i < num_elements; i++) {
// Create a row element <tr> of the table
let fila = document.createElement("tr");
// Create a <td> element and and create a text node with the content
// add the textnode inside the <td> element
// place the <td> element at the end of the table row
let celda = document.createElement("td");
let textoCelda = document.createTextNode(array_keys[i]);
celda.appendChild(textoCelda);
fila.appendChild(celda);
//
celda = document.createElement("td");
//decodeURIComponent(encoded.replace(/\+/g, " "));
textoCelda = document.createTextNode(decodeURIComponent(array_param[array_keys[i]].replace(/\+/g, " ")));
celda.appendChild(textoCelda);
fila.appendChild(celda);
// add the row to the end of the table (at the end of the tblbody element)
tblBody.appendChild(fila);
}
// position the <tbody> element below the <table> element
tabla.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tabla);
// modifies the "border" attribute of the table and sets it to "2";
tabla.setAttribute("border", "2");
tabla.setAttribute("font-size", "20");
textoCelda = document.createTextNode("This data can be saved in a database table on MySQL server or send an email with it or redirect to another web");
body.appendChild(textoCelda);
//========================================================================================
//example: send an email with data recieved with the user email client (outlook, etc..)
let mensaje = "These fields have been received from a form submit in a web page......\r\n";
//loop to read the arguments
for (let i = 0, num_elements = array_keys.length; i < num_elements; i++) {
mensaje += array_keys[i]+":\r\n";
mensaje += decodeURIComponent(array_param[array_keys[i]].replace(/\+/g, " "))+"\r\n";
}
//execute function to send an email
sendMail("Your Default Email Subject", mensaje);
//========================================================================================
}
}
</script>
<script>
// Execute the function
$_GET();
</script>
</body>
</html>
Comentarios sobre la versión: 1.1 (1)