Here is an example code snippet that utilizes Javascript to open a pop-up window:
If the pop-up is closed before pressing another button, the data is successfully sent and can be accessed inside the pop-up's javascript code using the following syntax:
window.onload = () => {
addAnotherTable(window["dataarray"]);
}
However, if the pop-up remains open when pressing another button, the window["dataarray"]
variable becomes undefined. Even though v_popup["dataarray"]
is still assigned to sub_array
within the main AddTable
function.
The question at hand is why does window["dataarray"]
become undefined without closing the pop-up, and how can this issue be resolved?
function addTable(result, indices) {
indices = [2, 3, 6, 2, 4, 4, 1, 5, 3, 1, 1, 1, 2, 4, 12, 1, 4, 4, 2, 1, 4, 1, 12, 13, 1]; //for testing
counter = 0;
low = [];
high = [];
for (var i = 0; i < indices.length; i++) {
let btn = document.createElement("button");
btn.innerHTML = result[counter][0];
btn.className = "button-28";
var low = counter;
var high = indices[i] + counter;
var sub_array = result.slice(low, high);
(function (sub_array) {
btn.onclick = function () {
var v_popup = window.open("popup.html", 'popUpWindow', 'height=300,width=700,left=50,top=50,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no, status=no');
v_popup["dataarray"] = sub_array;
}
})(sub_array);
counter += indices[i];
}
}