【问题标题】:Window opened won't close打开的窗口不会关闭
【发布时间】:2017-07-08 20:11:27
【问题描述】:

我有一个点击后会打开的窗口。

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');

} 

我认为这应该关闭同一个窗口。

 document.getElementById("closeIt").onclick = function (e) { 
 e.preventDefault();
 var popupManager = new PopupManager();
 popupManager.closeAll();
 popupManger.closeWindow();

 }

没有控制台错误,也没有任何反应。就是不会关闭。

我试图使用相同的 标记来使用 if else 语句打开和关闭窗口,但我无法弄清楚。所以现在我正在尝试使用两个不同的 标签,(是的,我知道这很愚蠢)一个打开一个关闭,但我什至不能这样做。

我什至无法弄清楚我在这里做错了什么。

任何帮助将不胜感激。叹息……

【问题讨论】:

  • 尝试打开同源窗口并告诉我结果
  • 我打开同源窗口;我已经为这个问题在 google.com 中添加了这个问题,这样这个问题就不会因为过于具体到该位置或类似的东西而被关闭。
  • 确定你关闭窗口在同一个脚本中打开它吗?还是从另一个脚本窗口本身?
  • 当您执行new PopupManager(); 时,您实例化了一个新实例,而this.windows 不再是您认为的那样。
  • @Mouneer,我正在从同一个文档和同一个脚本打开和关闭窗口。不涉及单独的页面或脚本。

标签: javascript function onclick window


【解决方案1】:

只需在包含窗口的 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 = {};

保持同一个变量可以跨实例访问。

【讨论】:

  • 谢谢!!这适用于我首先尝试使用的 if else 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多