简体   繁体   English

无模式窗口的行为类似于使用javascript的模式窗口

[英]modeless window to behave like a modal window using javascript

I have a requirement to have some modal popups appear on a webpage. 我要求在网页上显示一些模式弹出窗口。 At first i implemented them as true modal windows, but then i found out that true modal windows cannot communicate with parent window in IE. 最初,我将它们实现为真正的模式窗口,但是后来我发现真正的模式窗口无法与IE中的父窗口进行通信。

Now i am trying to implement them as regular windows that always steal focus, which is sorta working. 现在,我正在尝试将它们实现为常规窗口,这些窗口始终会抢占焦点,这在一定程度上是可行的。

Here is the code that i am using: 这是我正在使用的代码:

modalPopup = window.open(url, 'popup', arr.join(",")); //use a global var here
modalPopup.focus();

$(window).bind("focus.modal", function(){
  if(modalPopup){
    modalPopup.focus();
  } else {
    $(window).unbind("focus.modal");
  }
});

There are several things wrong with this: 这有几处错误:

  1. In firefox, once i close the popup, the modalPopup does not become null, it points to parent window. 在firefox中,一旦我关闭弹出窗口,modalPopup便不会变为空,而是指向父窗口。 (this is ok, since we dont support firefox anyway) (没关系,因为我们仍然不支持Firefox)
  2. In IE, it works like a charm when you open 1 window and close it, but opening any more windows results in the exception: 在IE中,当您打开1个窗口并将其关闭时,它的工作原理就像一个超级按钮,但是打开任何其他窗口都会导致异常:

    Error: The callee (server [not server application]) is not available and disappeared; 错误:被叫方(服务器[非服务器应用程序])不可用并消失; all connections are invalid. 所有连接均无效。 The call did not execute. 呼叫未执行。

edit : In IE the error happens when modalPopup.focus(); 编辑 :在IE中的错误发生在modalPopup.focus();。 is called. 叫做。 apparently modalPopup is never set to a falsy value when closed :P 显然,在关闭时,modalPopup从未设置为伪造的值:P

Can you help me write a better implementation that uses window.open for creating the popups? 您能帮我写一个更好的实现,使用window.open来创建弹出窗口吗?

Before anyone says anything, using lightbox is not an option . 在任何人说什么之前, 都不可以使用lightbox。 The popup windows contain A TON of html, javascript etc, and loading them in the DOM is not going to result in a good UX. 弹出窗口包含大量html,javascript等,并且将它们加载到DOM中不会带来良好的UX。 Also, we sorta have to have this work on IE6. 另外,我们必须在IE6上进行这项工作。

The windows containing a "ton" of JavaScript, HTML, etc. isn't a reason that you can't use "lightbox" style techniques (which do work on IE6; I don't know if a specific library you've looked at doesn't). 包含“大量” JavaScript,HTML等的窗口并不是您不能使用“灯箱”样式技术的原因(该技术在IE6上有效;我不知道您是否看过特定的库在不)。 The technique is simple: 该技术很简单:

  • Have an absolutely-positioned iframe on the page whose z-index is higher than any other content normally shown on the page. 在页面上放置绝对位置的iframe ,其i-index高于页面上通常显示的任何其他内容。 Normally the iframe is hidden. 通常, iframe是隐藏的。
  • When doing a "modal," show that iframe and set it to cover all other content. 执行“模式”时,请显示该iframe并将其设置为覆盖所有其他内容。 Create an absolutely-positioned div with a higher z-index than the iframe and place it wherever you want (typically in the middle of the viewport). 创建一个绝对定位的div ,该div的z索引比iframe ,并将其放置在任意位置(通常位于视口的中间)。
  • Put your "modal" content in that div. 将您的“模式”内容放在该div中。 This can be pre-loaded, or you can demand-load JavaScript and other resources to fill it. 可以预先加载它,也可以要求加载JavaScript和其他资源来填充它。
  • Have a UI control of some sort on the div that "closes" it by removing the div and hiding the iframe . div上有某种UI控件,通过移除div并隐藏iframe来“关闭”它。

You can build very rich UIs with this that (can) have a dramatically better UX than enforced multiple windows. 您可以使用它构建非常丰富的UI,与强制实施多个窗口相比,UX可以有更好的用户体验。 And you have the advantage of avoiding cross-windows communication and potentially offering much better response time to the user when they "open" one of these windows. 而且,您还具有避免跨窗口通信的优势,并且可以在用户“打开”这些窗口之一时为用户提供更好的响应时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM