javascript - 为什么 window.showModalDialog 被弃用?用什么代替?

标签 javascript modal-dialog privileges window.open showmodaldialog

我正在开发一个使用 window.showModalDialog 的 GreaseMonkey 脚本。

但在完成之前,我发现 Firefox 29 会发出警告:

Use of window.showModalDialog() is deprecated. Use window.open() instead. For more help https://developer.mozilla.org/en-US/docs/Web/API/Window.open

但问题是 window.open 需要 UniversalBrowserWrite 权限才能使用 window.open 打开模式窗口。

那么,为什么window.showModalDialog被弃用了?有没有不需要权限的API?

注意:我不需要一个fake模式对话框(就像jQuery的那样),我需要一个real模式来暂停JavaScript执行.

最佳答案

为什么 window.showModalDialog 被弃用?

来自http://tjvantoll.com/2012/05/02/showmodaldialog-what-it-is-and-why-you-should-never-use-it/ ,

In general the idea of putting a native dialog implementation into the browser was a really good idea, but window.showModalDialog was a bad implementation that is riddled with issues and poor browser support. (...)

Note that (...) [a modal dialog using showModalDialog] is a full HTML document, not a snippet that is injected in. This is a characterizing feature of window.showModalDialog. It’s really just two completely separate windows communicating with each other. The fact that you have two separate windows and DOMs means you don’t have to worry about JS & DOM conflicts, which is appealing if you have a lot of bad JavaScript with a cluttered global scope. But mostly this just adds unnecessary complexity, complicates the browser implementation, and contributes to a number of bugs. (...)

While it’s important that modal dialogs prevent the user from interacting with the originating window, there’s no reason the user shouldn’t be allowed to interact with other tabs or native browser controls (back/forward, favorites, address bar, etc). (...) This is actually a big annoyance to the end user. (...)

The debugging experience for window.showModalDialog is horrible. (...) You're basically forced to alert like it’s 1999 to determine what’s going on. (...)

Currently no major mobile browsers support window.showModalDialog, so if you’re looking for any sort of tablet / mobile support you need to stay away.

用什么代替?

HTML5 引入了新的 <dialog> 元素,可用于显示对话框,包括模式对话框。

例如:

<dialog id="myDialog">
    Foo bar
    <button id="hide">Close</button>
</dialog>
<button id="show">Show Dialog</button>
var dialog = document.getElementById('myDialog');  
document.getElementById('show').onclick = function() {  dialog.showModal();  };  
document.getElementById('hide').onclick = function() {  dialog.close();      };

Demo

问题是:

  • 浏览器支持目前可以忽略不计,但有 polyfill .
  • 没有pause JS 执行。

关于javascript - 为什么 window.showModalDialog 被弃用?用什么代替?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20733962/

相关文章:

javascript - 如何使用 HTML 和 JS 在本地保存图像?

javascript - 在 Node/Express 中散列、加盐并保存密码

jquery ui 对话框 ui-widget-overlay 高度问题

mysql - Ubuntu MySQL 服务器 - 权限和用户问题

windows - 在 Windows 下以访客身份或以有限权限运行应用程序而无需重新登录?

javascript - localStorage 返回对象

javascript - 多个指令需要共享相同的私有(private)范围

c# - Windows 应用商店应用程序中的异步自定义对话框

jquery - Bootstrap 模式对话框可以覆盖另一个对话框吗?

python - 作为另一个用户(例如 root)运行 Python 例程的 pythonic 方式是什么?