confirm

confirm

[14] DOM関数 alert(), confirm(), prompt() は、モーダルダイアログを表示します。

仕様書

定義

[32] alertconfirmprompt は、 Window インターフェイスメソッドです >>33

[35] ワーカーには存在しません。

[34] prompt は2つ、それ以外は1つの DOMString 引数をとりますが、いずれも省略可能で、その既定値は空文字列です >>33。 第1引数はメッセージ、第2引数は既定値を表します。

動作

[41] 次の場合は、実際にはダイアログを表示することなく、 呼び出し元に戻ることになっています。

[43] termination nesting level が非零の場合は、仕様上は処理を続行してもしなくても良いことになっています。 実際には現在では続行しないのが普通と思われます。

[19] モーダルダイアログ利用者の操作を妨害する使い方もできるので、 利用者の指示により、あるいは利用者エージェントの判断により何もしないことが認められているようです。 これが「その他」の場合です。

[17] Webブラウザーによっては、「これ以上ダイアログを表示しない」 のようなオプションがダイアログ上にあり、それを利用者が選択すると以後ダイアログを表示せずに呼び出し元に戻るようになっています。 スクリプトからはダイアログが表示されたかどうか確実に判断することはできません。

[16] ダイアログを表示する場合やその他の理由で表示しない場合 (>>18) には、まずストレージミューテックスを解放しなければなりません >>13。 ただし現状ストレージミューテックスを実装しているWebブラウザーはありません。

[20] 表示するダイアログは、次のようなものです。

[21] confirm()prompt() では、 ダイアログに対して利用者が返答するまで、イベントループpause しなければなりませんalert() では pause してもしなくても構いません。 >>13

[22] 現行 Webブラウザーalert() でも pause するようです。 しかしどちらの挙動か著者には厳密には判定できません。

[23] alert() は何も返さず、 confirm() は返答を真偽値で返し、 prompt() は返答を文字列で返します。 ダイアログを表示しなかった場合や中止された場合には、 それぞれnull を返します。 >>13

レンダリング

[24] ダイアログには、スクリプトが指定した文字列の一部または全部が表示されることが期待されています。

[26] かつては指定された文字列だけがプラットフォーム側の対応するダイアログ API に渡されていた時代もありましたが、現在の Webブラウザーではフィッシング防止などの理由でページの題名起源などの表示を付け加えた文字列がレンダリングされるようになっています。

[27] また並行する他の処理や異なる起源タブの扱いとモーダル性の両立のためか、 プラットフォーム側のダイアログ API を使わず、独自の方法でレンダリングする実装も増えています。

[28] ボタンは「OK」と「キャンセル」に相当する文字列 (プラットフォームによって地域化されたもの) を使うのが普通です。またボタンの順序やメッセージとの位置関係も、プラットフォームの慣習によります。

[29] confirm()ボタンは「はい」「いいえ」ではありませんが、 「はい」「いいえ」が回答になるような質問文がメッセージとして指定されることがよくあります。 一方で「○○ならOK、××ならキャンセル」のように「OK」や「キャンセル」のような特定のボタンのラベルを期待した質問文が使われることもあります。

[37] これらのダイアログには、スクリプトを停止させるオプションも用意することがすすめられています >>36

用法

[30] これらのダイアログはプラットフォームまたは Webブラウザーの提供するごく基本的な UI によりそのままレンダリングするだけのもので、表現力に乏しく、 Webサイトの視覚的デザインとの統合性にも欠けることから、 積極的に使うものではないと一般的には考えられています。その一方でその簡便さから、 機能の利用頻度や Webサイトにおける視覚的デザインの重要性その他の判断次第で、 これらのダイアログがそのまま採用されている場合も数多く存在します。

[31] alert() は、いわゆる printデバッグの手法としても非常によく用いられています。 ただし呼び出し回数が多い場合はダイアログを閉じる手間がかかることや、 イベントループとの総合作用を副作用として与えてしまうことから、 console.log を使うことも最近は増えてきています。 Webブラウザーによっては console.log で指定されたオブジェクトの内容をデバッグツールにより調べられるなど、 より便利になっています。

歴史

[1] Opera が唯一 javascript:for(;;)alert('); に対抗できるブラウザ - 冬通りに消え行く制服ガールは、夢物語にリアルを求めない。 - subtech ( 版) http://subtech.g.hatena.ne.jp/cho45/20091224/1261585798

[2] Issue 68780 - chromium - Disallow alert/confirm/prompt/showModalDialog calls issued from within an unload handler - Project Hosting on Google Code ( 版) http://code.google.com/p/chromium/issues/detail?id=68780

[3] javascript初心者なのですが、alert関数に設定できる文字数は何.. - 人力検索はてな ( 版) http://q.hatena.ne.jp/1306857041

[4] [whatwg] Can we deprecate alert(), confirm(), prompt() ? ( 版) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-June/031979.html

[5] [whatwg] Can we deprecate alert(), confirm(), prompt() ? ( ( 版)) http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2012-February/034695.html

[6] Web Applications 1.0 r6966 Allow browsers to bail early for showModalDialog, alert, confirm, and prompt during pagehide, beforeunload, and unload events. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6965&to=6966

[7] [whatwg] Can we deprecate alert(), confirm(), prompt() ? ( ( 版)) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-June/036332.html

[8] Web Applications 1.0 r7431 Allow UAs to implement abuse protection in alert() and company. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=7430&to=7431

[9] Web Applications 1.0 r7832 Make alert() and company match more browsers, and some internal notes. Sorry, lost track of which bugs this fixes due to build process issues. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=7831&to=7832

[11] Bug 563556 – Cleanup nsGlobalWindow's alert/confirm/prompt ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=563556

[12] [whatwg] Optional non-blocking mode for simple dialogs (alert, confirm, prompt). ( 版) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030670.html

[38] Bug 25686 – alert() needs two overloads so alert(undefined) can alert "undefined" ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=25686

[39] Web Applications 1.0 r8638 for compat, apparently alert() should alert and alert(undefined) should alert 'undefined']] ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8637&to=8638

[40] 社内SEの徒然なる日記 proxy.pac(プロキシパック)対応 No.8 pac作成(デバッグ) ( (たかたかハリコフ 著, 版)) http://harikofu.blog.fc2.com/blog-entry-517.html

[10] Block modal dialogs by default in sandboxed documents · whatwg/html@bbccfc9 ( 版) https://github.com/whatwg/html/commit/bbccfc976754def0c187ac8ce5891d2fb20dfc15

[47] Remove the storage mutex due to lack of implementation · whatwg/html@1b918cf ( 版) https://github.com/whatwg/html/commit/1b918cf72fcbba011f83b92ab5d1f483fb1cafa3

[48] Allow truncation of alert/confirm/prompt text · whatwg/html@56f5c5e ( 版) https://github.com/whatwg/html/commit/56f5c5e39b8b43b4bd4a8007fdc56ad797a5b2a7

[49] Issue 267101 - chromium - myIpAddress() in PAC script doesn't return expected result - Monorail ( ()) https://bugs.chromium.org/p/chromium/issues/detail?id=267101

[50] Use the Window's associated Document for allow-modals sandbox checks (domenic著, ) https://github.com/whatwg/html/commit/29ebd5b6e8c4ba4006115a784c5c6c87dc151489

[51] Add new neutral return-value option to window.confirm? · Issue #2838 · whatwg/html () https://github.com/whatwg/html/issues/2838

[52] New in Chrome 64  |  Web  |  Google Developers () https://developers.google.com/web/updates/2018/01/nic64

[53] Simplify Window.alert IDL · Issue #3822 · whatwg/html () https://github.com/whatwg/html/issues/3822

[54] Deprecations and removals in Chrome 69  |  Web  |  Google Developers () https://developers.google.com/web/updates/2018/08/chrome-69-deps-rems

[55] Treat CR, LF, CRLF the same for alert/confirm/prompt (domenic著, ) https://github.com/whatwg/html/commit/76638126838951c21aee4a1220cd0a4e57f3081d

[56] Remove alert(), confirm(), and prompt for cross origin iframes - Chrome Platform Status () https://www.chromestatus.com/feature/5148698084376576

[57] Chrome92からalert,confirmの動作の仕様変更でGASのWEBアプリで困った件 - Qiita () https://qiita.com/basictomonokai/items/928ca76bd1f985dd067d