Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if browser supports dialog

Posted here is an answer that instructs those who miss the old window.showModalDialog JavaScript function to use the

<dialog>

element instead. I have used this along with the polyfill needed for IE and FF and it works. However, there is a noticeable lag introduced when using the polyfill that I would like to avoid for Chrome (not to mention there is a warning not to use the polyfill when browsers support it). How do I detect whether or not the dialog element is supported so I can leave out the polyfill processing? Specifically these lines:

var dialog = document.getElementById('<element id>');
dialogPolyfill.registerDialog(dialog);
like image 795
Ian Avatar asked Aug 06 '15 01:08

Ian


People also ask

How do I know if my browser supports specific features?

The concept of feature detection The idea behind feature detection is that you can run a test to determine whether a feature is supported in the current browser, and then conditionally run code to provide an acceptable experience both in browsers that do support the feature, and browsers that don't.

What is a dialog on my browser?

A dialog is a box of content which is above the content of a page. The backdrop is something like a shadow behind an element which prohibits the interaction with the content behind it. Last but not least a modal is the combination of a dialog with a backdrop. Simply put: dialog + backdrop = modal.

How do I display a dialog box in HTML?

The <dialog> tag defines a dialog box or subwindow. The <dialog> element makes it easy to create popup dialogs and modals on a web page.


2 Answers

You could write a simple test like this:

if (typeof HTMLDialogElement === 'function') {
  /** yep */
} else {
  /** nope */
}
like image 120
colecmc Avatar answered Sep 21 '22 07:09

colecmc


Try console.log(typeof window.showModalDialog === 'undefined')

if (typeof window.showModalDialog === 'undefined') {
  console.log('No. ');
} else {
  console.log('Yes! ');
}
like image 20
iplus26 Avatar answered Sep 21 '22 07:09

iplus26