How can I pass data between two windows using javascript. I tried using global variable. But when the child window close, the variable value become undefined.
Here is parent js.
var allVariables;
function popupEmpSelect(id, code, name, display) {
var w = 800;
var h = 600;
var l = Number((window.screen.width - w) / 2);
var t = Number((window.screen.height - h) / 3);
var url = createUrl();
allVariables = window.open(url + '/dialog/empSelect/?displaySwitch='
+ display, 'empSelect',
'menubar=no, toolbar=no, location=no, width=' + w + ', height=' + h
+ ', top=' + t + ', left=' + l);
$(allVariables).on('unload', function () {
setTimeout(function () {
console.log(localStorage.getItem('data'));//The result become null here
console.log(allVariables);
console.log(allVariables.jsonObject);//**allVariables.jsonObject is always undefined here**
if ( !allVariables.closed ) return;
if ( typeof allVariables.jsonObject === 'undefined' ) return;
var val = JSON.parse(allVariables.jsonObject)[0];
console.log(allVariables.jsonObject);
if (!isElementEmpty(id)) $('#' + id).val(val.id);
if (!isElementEmpty(code)) $('#' + code).val(val.code);
if (!isElementEmpty(name)) $('#' + name).val(val.name);
if(display == 'true'){
collbackEmpSelect();
}
},10);
});
}
Here is child js.
var jsonObject;
$('button[name="btn_select"]').on('click', function(){
var json = new Array();
if ($('input[name="checked"]:checked').length == 0) {
setTimeout(function() {
$('#select1').focus();
}, 1);
return false;
}
let emp_No = '';
let emp_Name = '';
$('input[name="checked"]:checked').each(function() {
var s =$(this).val().split(",");
var $row = $(this).closest('tr');
var tmpEmpNo=$row.find(".empNo").text();
var tmpEmpName=$row.find(".empName").text();
emp_No += (emp_No != '' ? "," : "") + tmpEmpNo;
emp_Name += (emp_Name != '' ? "," : "") + tmpEmpName;
});
var checjed = {
"empNo":emp_No,
"empName":emp_Name
}
json.push(checjed);
jsonObject = JSON.stringify( json );
**localStorage.setItem('data', jsonObject);**
window.close();
});
The problem is jsonObject is always undefined when accessing from parent.js
What should I do to access jsonObject from parent.js