我正在尝试构建一个 jquery 脚本,它将为类似邮件的应用程序打开一个新的浏览器窗口(弹出窗口)(用户可以双击邮件,它们将在新窗口中打开)。
这并不是特别难。我的问题是我想跟踪打开的窗口,这样如果用户再次双击同一个邮件项目,它只会将焦点放在已经打开的弹出窗口上,而不是重新加载它。
我让它在 Firefox 中运行,但 Internet Explorer 返回以下错误:
Line: 51
Error: The interface is unknown.
特别是,当用户关闭一个弹出窗口,然后双击邮件项目再次打开它时,就会发生这种情况。
我的 javascript/jquery 技能充其量是初级的,所以我希望这里有人可以提供帮助。这是脚本的代码。
(function($)
{
var _popupTracker = {}
var _popupCounter = 0;
$.fn.openPopupWindow = function(options)
{
var defaults = {
height: 600, // sets the height in pixels of the window.
width: 600, // sets the width in pixels of the window.
toolbar: 0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
scrollbars: 0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
status: 0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
resizable: 1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
left: 0, // left position when the window appears.
top: 0, // top position when the window appears.
center: 0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
createnew: 0, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
location: 0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
menubar: 0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
};
var options = $.extend(defaults, options);
var obj = this;
// center the window
if (options.center == 1)
{
options.top = (screen.height - (options.height + 110)) / 2;
options.left = (screen.width - options.width) / 2;
}
var parameters = "location=" + options.location +
",menubar=" + options.menubar +
",height=" + options.height +
",width=" + options.width +
",toolbar=" + options.toolbar +
",scrollbars=" + options.scrollbars +
",status=" + options.status +
",resizable=" + options.resizable +
",left=" + options.left +
",screenX=" + options.left +
",top=" + options.top +
",screenY=" + options.top;
// target url
var target = obj.attr("href");
// test if popup window is already open, if it is, just give it fokus.
var popup = _popupTracker[target];
if (options.createnew == 0 && popup !== undefined && !popup.closed)
{
popup.focus();
}
else
{
var name = "PopupWindow" + _popupCounter;
_popupCounter++;
// open window
popup = window.open(target, name, parameters);
_popupTracker[target] = popup;
_popupTracker[target].focus();
}
return false;
};
})(jQuery);
给我错误的代码行是:
if (options.createnew == 0 && popup !== undefined && !popup.closed)
谢谢,埃吉尔。
更新:事实证明这实际上是 IE8 的东西,至少是 Windows 7 beta 中的版本。我建立了一个测试页面(http://egil.dk/popuptest/popup-source.htm),它似乎在我的同事 IE7 中按预期工作。嘎嘎,浪费时间!
更新2:我可能应该告诉如何重现错误。转到http://egil.dk/popuptest/popup-source.htm,单击其中一个链接,即“something 1”,弹出窗口完成加载后,返回父窗口,然后单击相同的链接再次。这一次,弹出窗口将再次获得焦点,而不是重新加载(这是故意的,也是我想要的)。现在关闭弹出窗口,然后再次单击相同的链接。这会在 IE8 beta 中产生错误。在 Firefox 中,它正确地重新打开。