javascript window.showmodaldialog 和 window.open 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3529698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:25:17  来源:igfitidea点击:

Difference between window.showmodaldialog and window.open

javascript

提问by Antoops

Can any one tell me the difference between these functions?

谁能告诉我这些功能之间的区别?

回答by Pekka

window.openopens a new independent window. It works in most browsers in some way (although pop-up blockers often block them or require additional user confirmation). It is possible to access the document that opened the window using the window.openerproperty.

window.open打开一个新的独立窗口。它以某种方式在大多数浏览器中工作(尽管弹出窗口阻止程序经常阻止它们或需要额外的用户确认)。可以使用该window.opener属性访问打开窗口的文档。

showModalDialogopens a dialog that is tied to the current page. It is impossible to do anything on the page until the dialog gets closed. (MSDN docs)

showModalDialog打开一个与当前页面相关联的对话框。在对话框关闭之前,无法在页面上执行任何操作。( MSDN 文档)

The most important distinction between the two is that showModalDialoghalts the execution of the JavaScript until the dialog box is closed, and can return a return value(at least in Internet Explorer). In that, it works similar e.g. to the confirm()dialog. In contrast, window.openopens a window "asynchronously": script execution will continue immediately, even while the new window loads.

两者之间最重要的区别是showModalDialog在对话框关闭之前暂停 JavaScript 的执行,并且可以返回一个返回值(至少在 Internet Explorer 中)。在这方面,它的工作方式类似于confirm()对话框。相反,window.open“异步”打开一个窗口:脚本执行将立即继续,即使在新窗口加载时也是如此。

It is possible to access the parent document from the dialog with some property whose name I can't remember right now, but it is different from window.open.

可以从对话框中访问父文档,其中包含一些我现在不记得其名称的属性,但它与window.open.

One more thing to note is that in my experience, modal dialogs are awfully difficult to refresh, as they seem to be subject to different caching rules than normal pages. The F5 key won't work to refresh the page. One workaround is to use a random addition to the loaded URL every time (pagename.htm?random=1203402920)

需要注意的另一件事是,根据我的经验,模态对话框非常难以刷新,因为它们似乎受制于与普通页面不同的缓存规则。F5 键无法刷新页面。一种解决方法是每次都使用随机添加到加载的 URL ( pagename.htm?random=1203402920)

In general, seeing as showModalDialogis a proprietary function and its functionality can't be easily ported to other browsers, it's usually best not to use it.

一般来说,由于showModalDialog是专有功能,其功能不能轻易移植到其他浏览器,通常最好不要使用它。

回答by MrEdmundo

From MSDN

来自 MSDN

A modal dialog box retains the input focus while open

模式对话框在打开时保留输入焦点

This means that while it's open the user cannot obtain focus on the Window it was opened from. Window.Open will create a new Window.

这意味着当它打开时,用户无法获得打开它的窗口的焦点。Window.Open 将创建一个新窗口。

回答by Sam

Showmodaldialog will do exactly as it says, show a modaldialog, ie a window that the user needs to close first before he can get back to the original window, window.open will just open a new window, but the user is free to ignore this and get back to the original window whenever he wants.

Showmodaldialog 将完全按照它说的做,显示一个模态对话框,即用户需要先关闭一个窗口才能返回原始窗口,window.open 将只打开一个新窗口,但用户可以随意忽略这并在他想要的时候回到原来的窗口。