aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/window/confirm/index.html
blob: 5f021b77e9006101318d1d604893520d6cab9bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: Window.confirm()
slug: Web/API/Window/confirm
tags:
  - API
  - DOM
  - React-Router
  - Window
  - Window.confirm()
translation_of: Web/API/Window/confirm
---
<div>{{ApiRef("Window")}}</div>

<p><code><strong>Window.confirm()</strong></code> 方法显示一个具有一个可选消息和两个按钮(确定和取消)的模态对话框 。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre><em>result</em> = window.confirm(<em>message</em>);
</pre>

<ul>
 <li>message 是要在对话框中显示的可选字符串。</li>
 <li>result 是一个布尔值,表示是选择确定还是取消 (true表示OK)。</li>
</ul>

<h2 id="Example" name="Example">示例</h2>

<pre>if (window.confirm("Do you really want to leave?")) {
  window.open("exit.html", "Thanks for Visiting!");
}
</pre>

<p>运行结果: </p>

<p><img alt="firefox confirm" src="https://mdn.mozillademos.org/files/7163/firefoxcomfirmdialog_zpsf00ec381.png" style="height: 119px; width: 354px;"><br>
  </p>

<h2 id="Notes" name="Notes">注意事项:</h2>

<p><span class="comment">The following text is shared between this article, DOM:window.prompt and DOM:window.alert</span>对话框是弹出式(modal)的(也即alert样式, 译者注). 直到这个对话框被点击后, 后面的脚本才会运行. 请勿滥用此功能, 这里列出了很多个理由: <a href="http://alistapart.com/article/neveruseawarning">请放弃使用对话框来确认信息</a>.</p>

<p><a href="/en-US/Chrome" title="Chrome">Mozilla Chrome</a> 的用户(比如Firefox插件开发者)应该使用{{interface("nsIPromptService")}}这个方法.</p>

<p>Chrome浏览器版本 {{CompatChrome(46.0)}} 开始屏蔽内部{{htmlelement("iframe")}}元素, 除非用户在沙箱内开启了<code>allow-modal选项.</code></p>

<p>{{gecko_minversion_inline("23.0")}}内核中, 参数是可选的.</p>

<h2 id="Specification" name="Specification">兼容性</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 Web application', '#dom-confirm', 'confirm()')}}</td>
   <td>{{Spec2('HTML5 Web application')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="See_also" name="See_also">更多</h2>

<ul>
 <li>{{domxref("window.alert","alert")}}</li>
 <li>{{domxref("window.prompt","prompt")}}</li>
</ul>