2

我正在尝试构建一个 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 中,它正确地重新打开。

4

3 回答 3

4

我注意到的唯一一件事是,您的默认菜单栏后有一个额外的逗号。删除最后一个逗号后,它在 IE7 上对我来说效果很好。如果 IE 给你这个问题是什么版本?

于 2009-02-03T19:51:42.047 回答
1

以下似乎对我有用。如果有人可以改进它,请做!

在 Firefox 中,弹出窗口只有在打开时才会获得焦点。在 IE8 中,“接口未知”错误被捕获,弹出窗口被关闭并重新打开。在所有情况下,window.open 行都会将当前“页面”加载到弹出窗口中。

变种比吉姆;// 在函数外部设置这个变量,所以第一次
             // 弹出窗口打开,bigimg 是一个已定义的变量,并且 if 不会阻塞。

功能弹出大图片(页面)
  {
  尝试
    {
    if(window.focus && bigimg) bigimg.focus();
    }
  抓住(错误)
    {
    if(bigimg) bigimg.close();
    }
  bigimg = window.open(page,"popup","width=670,height=665,toolbar=no");
  }
于 2010-08-26T17:10:02.103 回答
0

我遇到了 IE 8 的问题,即 window.open 无法正常工作,我只是将窗口名称替换为 null,我从http://msdn.microsoft.com/en-us/library/ms536651.aspx得到了想法

window.open("Sample.htm",null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");

于 2009-04-03T11:00:08.383 回答