我正在尝试构建一个 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”,弹出窗口完成加载后,按 Tab 键返回到父窗口,然后再次单击同一链接。这次,弹出窗口将再次获得焦点,而不是重新加载(这是故意的,也是我想要的)。现在关闭弹出窗口,然后再次单击同一链接。这会在 IE8 beta 中产生错误。在 Firefox 中,它可以正确重新打开。
最佳答案
我注意到的唯一一件事是你的默认菜单栏后面有一个额外的逗号。删除最后一个逗号后,它在 IE7 上对我来说工作得很好。如果 IE 出现此问题,是什么版本?
关于javascript - 需要 jquery/javascript 弹出窗口脚本的帮助 - 在 Internet Explorer 中出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/507968/