如何创建一个返回值的crossbrowser模式对话框 [英] How can you create a crossbrowser modal dialog that returns values

查看:70
本文介绍了如何创建一个返回值的crossbrowser模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我遇到了一个大问题。



我必须创建功能,你可以有一个模态对话框,返回一个值(不是是或否按钮配置),你从网格中选择一个项目(所以这是一个HTML页面)。



该功能已经适用于IE7及以下版本。这是因为它使用了不推荐使用的showModalDialog函数。我的工作是使用最新的浏览器以及IE7。它应该仍然适用于IE7,因为我们将它分段推出而不是整个网站。最终整个网站应该能够支持新的浏览器。



我想知道是否有人有解决方案。



亲切的问候。

丹尼斯



我尝试过:



我尝试过使用window.open,但代码在选择值之前不会暂停。

Hi there.

I am sitting with a big problem.

I have to create functionality where you can have a modal dialog that returns a value (not a yes or no button configuration) where you choose an item from a grid (so this is an html page).

The functionality already works for IE7 and below. This is because it uses the showModalDialog function which is deprecated. My job is for it to work on the latest browsers as well as IE7. It should still work for IE7 because we are rolling it out in segments and not the whole site at once. Eventually the whole site should be able to support the new browsers.

I was wondering if someone has a solution.

Kind regards.
Dennis

What I have tried:

I have tried using window.open but the code does not pause until the value is chosen.

推荐答案

整个想法是错误。我的意思是,这不仅是可能的,也是不可能的;没有这样的概念,也不可能。这不是事情的运作方式。窗口或模态对话框不是一个功能;你什么都没有回来。此外,你不需要任何回报,也没有电话。



这很难解释,但也许你会理解它更好地理解基础知识。为了这个理解,你甚至不需要或不了解ASP.NET;只需在单个会话中理解生命周期的客户端部分就足够了;浏览器如何使用HTML和JavaScript。您还需要了解面向事件的范例。



当然,您绝不应该使用已弃用的 showModalDialog 。为什么你认为它被弃用了?因为这个想法是错的。



没有什么可以暂停,与确认相反(这是一件非常糟糕的事情) ;我不会在生产级产品中使用它。你有一个很大的活动周期;你操纵页面,它响应事件。然后你创建另一个窗口,它有自己的循环,所以两个窗口都在它们的循环中工作。您可以同时使用它们。你甚至根本没有模态行为。你只能通过禁用原始页面上的所有控件来模仿它(苍白的模仿,无论如何);但为什么?你真的需要清楚你对事物的理解。



但现在,你应该更好地接受 window.open 。在大多数情况下,不仅对用户不方便,而且还有两种麻烦1)许多用户会在浏览器中阻止此行为,2)一般情况下,您无法以编程方式关闭此窗口;当相同的浏览器窗口已经有标签时,如果它作为浏览器的标签打开,你将面临这个问题。



我强烈建议使用不同的概念,所谓的<我>模态弹出。当然,也不是回报。我试着在我的文章 Modal Popup From Scratch 中解释一切。



在所有情况下,即使你使用 window.open (再次,不要这样做;你可以这样做在某些特殊情况下,但将其用作对话框并不是一个好主意)您想要暂停,阻止调用或返回不起作用。真正的面向事件的行为实际上比你想象的行为要好得多。



这是你基本上做的事情:比方说,你有两种状态的HTML / JavaScript application:模态状态,或具有两个窗口的状态,以及正常状态。当您转换回正常状态时,您将处理导致它的一些事件。例如,用户单击某个按钮。当模态状态结束时,它可以是在所有情况下调用的特殊事件。你处理它,这就是你立即采取行动的地方。



参见:

控制权的反转 - 维基百科,免费的百科全书

Event-driven programming - Wikipedia,the free encyclopedia



很简单,这是关于控制反转。在你的情况下,假设你有一个函数 doSomething(yesOrNo)。这是错误的做事方式:

The whole idea is wrong. I mean, it's not just possible or impossible; there is no such concept and cannot be. This is not how things work. A window or a modal dialog is not a function; you have nothing to return from. Moreover, you don't need anything to return from, and you don't have a call.

It's hard to explain, but perhaps you will understand it when you understand the basics better. For this understanding, you don't even need or understand ASP.NET; it's enough to understand just the client-side part of the life cycle, in a single session; how a browser works with HTML and JavaScript. You also need to understand the event-oriented paradigm.

And of course you should never use deprecated showModalDialog. Why do you think it was deprecated? Because the idea was wrong.

Nothing pauses nothing, in contrast to confirm (which is a pretty bad thing; I would not use it in a production-grade product). You have a big event cycle; you manipulate the page, it responds to event. Then you create another window, it has its own cycle, so both windows work in their cycles. You can work with both at the same time. You don't even have modal behavior at all. You can only mimic it (pale imitation, anyway) by disabling all controls on the original page; but why? You really need to clear your understanding of things.

But now, you should better receive the whole idea of window.open. Not only it is inconvenient for the users in most cases, but you also can have two kinds of troubles 1) many users will block this behavior in browser, 2) in general case, you cannot programmatically close this window; you will face this problem if it is opened as a browser's tab when the same browser window already have tabs.

I would strongly advice using a different concept, so called modal popup. Of course, not returns as well. I tried to explain everything in my article Modal Popup From Scratch.

In all cases, even if you use window.open (again, don't do it; you can possibly do it in some special cases, but its not a good idea to use it as a dialog) you idea of pausing, or blocking call, or "return" does not work. The real event-oriented behavior is actually much better than your imaginary behavior.

Here is what you basically do: let's say, you have two states of your HTML/JavaScript application: a "modal" state, or a state with two windows, and a "normal" state. When you make a transition back to "normal" state, you handle some events causing it. For example, the user clicks some button. It can be a special event invoked in all cases when the "modal state" ends. You handle it, and this is the point where you immediately do some action.

See also:
Inversion of control — Wikipedia, the free encyclopedia,
Event-driven programming — Wikipedia, the free encyclopedia.

Very briefly, this is about inversion of control. In your case, let's say you have a function doSomething(yesOrNo). This is wrong way of doing thing:
var yesOrNo = showDialog(); // there is no such thing
doSomething(yesOrNo);
// this is wrong, because the code immediately calls some function;
// but you cannot have the point in your code
// where you can execute this fragment



和写方式应该是什么喜欢


and write way should be something like

myModalPopup.onModalStateEnds = function() {
   var yesOrNo = myModalPopup.getState();
   doSomething(yesOrNo);
}



在第二个片段中,未调用函数 doSomething 。相反,此代码更改了对象 myModalPopup 的事件处理。如果不立即调用该函数,它会说:当你退出'模态状态'时,调用这个函数......这样的东西可以用于模态弹出,但是在单独的浏览器窗口的情况下(我讨厌重复它)第三次,但我会再说一遍:最好不要这样做。)你必须在新窗口中创建一些属性来传递一些关于最后要做什么的知识,可能是事件属性(一个函数对象) 。



-SA


In the second fragment, your function doSomething is not called. Instead, this code changes the event handling of the object myModalPopup. Without calling the function immediately, it says: "when you go out of 'modal state', call this function…" Something like that can be done for modal popup, but also in case of a separate browser window (I hate to repeat it third time, but I'll say again: better don't do it.) You would have to create some property in a new window to pass some knowledge on what to do at the end, possibly the event property (a function object).

—SA


这篇关于如何创建一个返回值的crossbrowser模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆