如何检测浏览器是否支持对话框

How to detect if browser supports dialog

已发布 here 是一个答案,指示那些怀念旧 window.showModalDialog JavaScript 功能的人使用

<dialog>

元素代替。我已经将它与 IE 和 FF 所需的 polyfill 一起使用并且它有效。但是,在使用 polyfill 时引入了明显的延迟,我想为 Chrome 避免这种情况(更不用说在浏览器支持时有不要使用 polyfill 的警告)。如何检测对话框元素是否受支持,以便我可以省去 polyfill 处理?特别是这些行:

var dialog = document.getElementById('<element id>');
dialogPolyfill.registerDialog(dialog);

尝试console.log(typeof window.showModalDialog === 'undefined')

if (typeof window.showModalDialog === 'undefined') {
  console.log('No. ');
} else {
  console.log('Yes! ');
}

您可以像这样编写一个简单的测试:

if (typeof HTMLDialogElement === 'function') {
  /** yep */
} else {
  /** nope */
}

function dialogElementSupported() {
  return typeof document.createElement('dialog').show === 'function';
}