Sub topic 2
Modal versus Non-Modal: show() and showModal()
This chapter assumes you know basic HTML forms, CSS selectors, and how to select elements and attach event listeners in JavaScript. It covers the one choice that shapes everything else about a <dialog>: whether it opens as a modal or as a non-modal.
Two ways to open the same element
A <dialog> element is hidden until it is open. You open it from JavaScript with one of two methods, and the method you pick decides how the dialog behaves.
show()opens a non-modal dialog. The page behind it stays interactive.showModal()opens a modal dialog. The rest of the page becomes inert, meaning it cannot be clicked, focused, or read by assistive technology until the dialog closes.
Both methods add the open attribute to the element. Both are closed with close().
What changes between the two
| Behavior | `show()` | `showModal()` |
|---|---|---|
| Rest of the page | Stays interactive | Inert |
| Backdrop (`::backdrop`) | None | Yes |
| Escape key closes it | No | Yes |
| Stacking | Normal stacking order | Top layer, above everything |
| Focus | Not moved automatically | Moved into the dialog |
| Matches `:modal` | No | Yes |
The details of the top layer and focus are covered in later chapters. For now, the key point is that a modal dialog demands an answer before the user can continue, and a non-modal dialog does not.
Seeing both in code
This example opens the same kind of dialog both ways. Run it in a modern browser and compare.
<button id="open-plain">Open non-modal</button>
<button id="open-modal">Open modal</button>
<dialog id="plain">
<p>This dialog is non-modal.</p>
<button onclick="this.closest('dialog').close()">Close</button>
</dialog>
<dialog id="modal">
<p>This dialog is modal.</p>
<button onclick="this.closest('dialog').close()">Close</button>
</dialog>
<script>
const plain = document.getElementById('plain');
const modal = document.getElementById('modal');
document.getElementById('open-plain')
.addEventListener('click', () => plain.show()); // page stays interactive, no backdrop
document.getElementById('open-modal')
.addEventListener('click', () => modal.showModal()); // top layer, backdrop, page inert
</script>After opening the non-modal dialog, you can still click both "Open" buttons. After opening the modal one, you cannot. Pressing Escape closes only the modal.
Choosing between them
Use showModal() when the user must finish or dismiss something before continuing. Typical cases are confirmations, edit forms, and login prompts.
Use show() when the content is supplementary and the user may want to keep working. Typical cases are a floating help panel, a non-blocking notification with controls, or a small tool window.
If you are unsure, ask whether ignoring the dialog would be a reasonable thing for the user to do. If it would, use show().
The open attribute is not the same thing
You can also set the open attribute directly, in HTML or with dialog.setAttribute('open', ''). This produces a non-modal dialog, the same as show(). It skips the modal behavior entirely. For that reason, prefer the methods over the attribute, and never use the attribute when you want a modal.
You can check the current state with the open property:
const dlg = document.getElementById('modal');
console.log(dlg.open); // false before opening
dlg.showModal();
console.log(dlg.open); // true
console.log(dlg.matches(':modal')); // true only for showModal()Common errors
Calling showModal() on a dialog that is already open throws an InvalidStateError. This happens most often when a button is clicked twice, or when a dialog was first opened with show() and then upgraded. Guard the call:
function openModal(dlg) {
if (!dlg.open) {
dlg.showModal();
}
}Calling show() on a dialog that is already open does nothing if it was opened non-modally, but throws if it was opened as a modal. You cannot switch an open dialog between the two modes. Close it first, then reopen it with the other method.
Exercise
A dialog was opened with dialog.show(). A teammate then calls dialog.showModal() on it to "make it modal". What happens, and how should the code be fixed?
Show answer
showModal() throws an InvalidStateError because the dialog is already open, so it stays non-modal. Fix it by calling dialog.close() first and then dialog.showModal(), or by choosing the correct method at the original call site.
Exercise
You add <dialog open> to your HTML for a confirmation prompt. The prompt appears, but the page behind it is still clickable and Escape does nothing. Why, and what is the fix?
Show answer
The open attribute opens the dialog as a non-modal, so there is no top layer, backdrop, inertness, or Escape handling. Remove the attribute and call showModal() from JavaScript.