需要 jquery/javascript 弹出窗口脚本的帮助 - 在 Internet Explorer 中出现奇怪的错误

发布于 2024-07-12 08:33:28 字数 4092 浏览 4 评论 0原文

我正在尝试构建一个 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)

谢谢,Egil。

更新:事实证明,这实际上是 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 中它可以正确重新打开。

I am trying to build a jquery script that will open a new browser window (popup window) for a mail like application (user can double click on mails and they will open in a new window).

That is not particular hard. My problem is that I want to keep track of the opened windows, so that if a user double clicks on the same mail item a second time, it will just set focus on the already open popup window, and not reload it.

I have it working in Firefox, but Internet Explorer returns the following error:

Line: 51
Error: The interface is unknown.

In particular, it happens when the users has closed a popup window, and then double click on the mail item to open it again.

My javascript/jquery skills are rudimentary at best, so I hope somebody here can help out. Here is the code for the script.

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

The line of code that is giving me the error is:

if (options.createnew == 0 && popup !== undefined && !popup.closed)

Thanks, Egil.

UPDATE: Turns out that this is in fact a IE8 thing, at least the version in the Windows 7 beta. I put up a test page (http://egil.dk/popuptest/popup-source.htm) and it seems to work as expected in from my colleagues IE7. Gah, time wasted!

UPDATE 2: I should probably tell how to reproduce the error. Go to http://egil.dk/popuptest/popup-source.htm, click on one of the links, i.e. "something 1", after the popup finishes loading, tab back to to parent window, and click on the same link again. This time, the popup window will just receive focus again, and NOT reload (this is intentionally and what I want). Now close the popup window, and then click on the same link again. This produces the error in the IE8 beta. In Firefox it correctly reopens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆啦不做梦 2024-07-19 08:33:28

我唯一注意到的是你的默认菜单栏后面有一个额外的逗号。 删除最后一个逗号后,它在 IE7 上对我来说工作得很好。 如果 IE 出现此问题,是什么版本?

The only thing that I noticed is that you have an extra comma after menubar in your defaults. After removing that last comma it worked fine for me on IE7. What version if IE is giving you this problem?

生活了然无味 2024-07-19 08:33:28

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

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

var bigimg;  // set this variable outside the function so the first time 
             // the popup opens, bigimg is a defined variable and the if's don't choke.

function popupbigpic(page) 
  {
  try
    {
    if(window.focus && bigimg) bigimg.focus(); 
    }
  catch(err)
    {
    if(bigimg) bigimg.close();
    }
  bigimg = window.open(page,"popup","width=670,height=665,toolbar=no");
  }

The following seems to be working for me. If anyone can improve it, please do!

In Firefox, the popup just gets focus if it's open. In IE8, the "interface is unknown" error is caught and the popup is closed and reopened instead. In all cases, the window.open line loads the current 'page' into the popup.

var bigimg;  // set this variable outside the function so the first time 
             // the popup opens, bigimg is a defined variable and the if's don't choke.

function popupbigpic(page) 
  {
  try
    {
    if(window.focus && bigimg) bigimg.focus(); 
    }
  catch(err)
    {
    if(bigimg) bigimg.close();
    }
  bigimg = window.open(page,"popup","width=670,height=665,toolbar=no");
  }
幽梦紫曦~ 2024-07-19 08:33:28

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

window.open("Sample.htm",null,
“高度=200,宽度=400,状态=是,工具栏=否,菜单栏=否,位置=否”);

I had issue with IE 8 that window.open was not working, I just replaced the window name with null, I got idea from 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");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文