如何解决'onunload' 事件关闭弹出窗口
我正在使用以下代码在页面上生成图像(从 .ejs 页面中传递的数组)和“单击”图像时页面上的“弹出”窗口:
addEventListener("load",initialize);
addEventListener("onunload",tidy);
const vitals = document.querySelector('#passed'); //reference the 'passed' information to the page
const _convert = vitals.dataset.pix.split(",");
//convert the passed ('gallery_list') string into an array...
var _limits = vitals.dataset.size;
//set limit to # of items in 'gallery_list' array...
var generator;
function tidy() {
console.log("FIRED UNLOAD: ");
generator.window.close(); // Closes the new window
}
function initialize() {
var _count = 0,_str = vitals.dataset.username;
var _tally = 0; //'count' the # of 'containers' and 'rows' in usage...
if (vitals.dataset.size > 20) {
_limits = 20;
} //limit to 20 items per page...
for (_count = 0; _count < _limits; _count++) {
//**CREATE WITHIN PROPER 'COLUMNS'...WHEN APPLICABLE...
if ((_count === 0) || (Number.isInteger(_count / 5) == true)) {
if (_count != 0) {
_tally += 1
}
}
//**CREATE CONTENT HERE...
//**ADD THE PHOTO...
_item = document.createElement("img");
_item.setAttribute("class","gallery_pix");
_item.setAttribute("id","pix_" + _str);
_item.setAttribute("src","/galleries/pix/" + _convert[_count]);
_item.setAttribute("alt","picture of " + _str);
_item.setAttribute("width",'100%');
document.getElementById("group_" + _tally).appendChild(_item);
//**
//**
} //for loop
//Get an array of photo images from the page
var _photos = document.querySelectorAll(".gallery_pix");
//Loop through the resulting array
for (var i = 0; i < _photos.length; i++) {
_photos[i].addEventListener("click",function() {
var _img_select = this.src;
var _img_id = this.id;
var _splits = _img_id.search("_"); //return the position of the FirsT "underscore ('_')
_pix_name = _img_id.substring(_splits + 1); //return the 'name' of the clicked image
//**CREATE A POPUP WINDOW...WHEN AN IMAGE IS 'CLICKED'
generator = window.open('','pops','height=400,width=500');
generator.document.write('<html><head><title>Popup</title>');
generator.document.write('<link rel="stylesheet" href="style.css">');
generator.document.write('</head><body>');
generator.document.write("<div>");
generator.document.write("<h1>Out with the old,in with the new!</h1>");
generator.document.write("</div>");
generator.document.write('</body></html>');
generator.document.close();
var sheet = generator.document.createElement('style')
sheet.innerHTML = "div {border: 2px solid black; background-image: _img_select; background-color: blue;}";
generator.document.body.appendChild(sheet);
//**
});
} //for loop
}
弹出窗口已成功创建,但在页面更改时不会关闭。 'onunload' 的事件侦听器似乎没有执行...因为我没有在控制台中看到我的消息来表明该函数已被触发...
非常感谢任何建议。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。