Newly available 主要ブラウザで対応済みだが、古いバージョンでは未対応の場合がある。

対応ブラウザ

機能 デスクトップ モバイル
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
134
134
139
18.4
134
18.4
1+対応 (バージョン) 未対応 注釈あり サブ機能の解説は MDN Web Docs (CC BY-SA 2.5)

基本構文

JAVASCRIPT
const dialog = document.getElementById('myDialog');
// Close request that can be canceled
dialog.addEventListener('cancel', (e) => {
  if (!confirm('Are you sure you want to close this?'')) e.preventDefault();
});
dialog.requestClose();

ライブデモ

Basic request close

Call requestClose on a dialog and fall back to close when the method is unavailable.

プレビュー全画面表示

Intercept the cancel path

Listen for the cancel event before allowing the close request to complete.

プレビュー全画面表示

Request close result

Show the dialog returnValue after a scripted close request completes.

プレビュー全画面表示

実務での使いどころ

  • dialog.requestClose() の活用

    requestClose() は HTMLDialogElement インターフェイスのメソッドで、<dialog> を閉じる要求を行います。

注意点

  • 古いブラウザでは対応していない場合がある。

アクセシビリティ

  • スクリーンリーダーでの読み上げを確認すること。