Window 打开不会关闭
Window opened won't close
我有一个 window 点击打开。
function PopupManager() {
this.name = "_popupmanager_";
this.windows = {};
};
PopupManager.prototype.open = function(url, option, size, name) {
var url = "http://google.com"
var option = "null, status=no, toolbar=no, menubar=no, titlebar=no,
location=no, scrollbars=no, resizable=no"
var size = 'height=200, width = 814'
this.windows[name] = window.open(url, option, size, name);
this.windows[name].focus();
};
PopupManager.prototype.closeAll = function() {
for (name in this.windows) {
this.closeWindow(name);
}
}
PopupManager.prototype.closeWindow = function(name) {
if (this.windows[name]) {
if (!this.windows[name].closed) {
this.windows[name].opener.name="indexpage";
this.windows[name].close();
}
delete this.windows[name];
}
};
//初始化
document.getElementById("popupManager").onclick = function (e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.open('http://www.google.com', 'google');
}
而且我认为这应该同样关闭 window。
document.getElementById("closeIt").onclick = function (e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.closeAll();
popupManger.closeWindow();
}
没有控制台错误,也没有任何反应。只是不会关闭。
我试图使用相同的 标签打开和关闭 window 使用 if else 语句,但我无法弄清楚。所以现在我正在尝试使用两个不同的 标签,(是的,我知道这很愚蠢)一个用于打开,一个用于关闭,但我什至不能这样做。
我什至不知道我做错了什么。
任何帮助将不胜感激。叹息...
只需在保存 windows 的 popupManager 外部定义一个变量,以便您稍后可以访问它们以关闭。
您也可以将其包装为 IIFE 以创建闭包。
var windows = {};
function PopupManager() {
this.name = "_popupmanager_";
};
PopupManager.prototype.open = function(url, option, size, name) {
var url = "http://google.com"
var option = "null, status=no, toolbar=no, menubar=no, titlebar=no, location = no, scrollbars = no, resizable = no ";
var size = 'height=200, width = 814'
windows[name] = window.open(url, option, size, name);
windows[name].focus();
};
PopupManager.prototype.closeAll = function() {
for (name in windows) {
this.closeWindow(name);
}
}
PopupManager.prototype.closeWindow = function(name) {
if (windows[name]) {
if (!windows[name].closed) {
windows[name].opener.name = "indexpage";
windows[name].close();
}
delete windows[name];
}
};
document.getElementById("popupManager").onclick = function(e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.open('http://www.google.com', 'google');
}
document.getElementById("closeIt").onclick = function(e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.closeAll();
popupManager.closeWindow();
}
<button id="popupManager">Open</button>
<br />
<button id="closeIt">Close</button>
另一种选择是使用
PopupManager.prototype.windows = {};
保持同一个变量可以跨实例访问。
我有一个 window 点击打开。
function PopupManager() {
this.name = "_popupmanager_";
this.windows = {};
};
PopupManager.prototype.open = function(url, option, size, name) {
var url = "http://google.com"
var option = "null, status=no, toolbar=no, menubar=no, titlebar=no,
location=no, scrollbars=no, resizable=no"
var size = 'height=200, width = 814'
this.windows[name] = window.open(url, option, size, name);
this.windows[name].focus();
};
PopupManager.prototype.closeAll = function() {
for (name in this.windows) {
this.closeWindow(name);
}
}
PopupManager.prototype.closeWindow = function(name) {
if (this.windows[name]) {
if (!this.windows[name].closed) {
this.windows[name].opener.name="indexpage";
this.windows[name].close();
}
delete this.windows[name];
}
};
//初始化
document.getElementById("popupManager").onclick = function (e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.open('http://www.google.com', 'google');
}
而且我认为这应该同样关闭 window。
document.getElementById("closeIt").onclick = function (e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.closeAll();
popupManger.closeWindow();
}
没有控制台错误,也没有任何反应。只是不会关闭。
我试图使用相同的 标签打开和关闭 window 使用 if else 语句,但我无法弄清楚。所以现在我正在尝试使用两个不同的 标签,(是的,我知道这很愚蠢)一个用于打开,一个用于关闭,但我什至不能这样做。
我什至不知道我做错了什么。
任何帮助将不胜感激。叹息...
只需在保存 windows 的 popupManager 外部定义一个变量,以便您稍后可以访问它们以关闭。
您也可以将其包装为 IIFE 以创建闭包。
var windows = {};
function PopupManager() {
this.name = "_popupmanager_";
};
PopupManager.prototype.open = function(url, option, size, name) {
var url = "http://google.com"
var option = "null, status=no, toolbar=no, menubar=no, titlebar=no, location = no, scrollbars = no, resizable = no ";
var size = 'height=200, width = 814'
windows[name] = window.open(url, option, size, name);
windows[name].focus();
};
PopupManager.prototype.closeAll = function() {
for (name in windows) {
this.closeWindow(name);
}
}
PopupManager.prototype.closeWindow = function(name) {
if (windows[name]) {
if (!windows[name].closed) {
windows[name].opener.name = "indexpage";
windows[name].close();
}
delete windows[name];
}
};
document.getElementById("popupManager").onclick = function(e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.open('http://www.google.com', 'google');
}
document.getElementById("closeIt").onclick = function(e) {
e.preventDefault();
var popupManager = new PopupManager();
popupManager.closeAll();
popupManager.closeWindow();
}
<button id="popupManager">Open</button>
<br />
<button id="closeIt">Close</button>
另一种选择是使用
PopupManager.prototype.windows = {};
保持同一个变量可以跨实例访问。