You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Add sandbox attrs to live samples that call prompt() and confirm() (#38253)
* feat(api): better example for window.prompt
* feat(api): better example for prompt()
* feat(api): better example for prompt()
* feat(api): better example for prompt()
* feat(api): Revert notes changes to help reviewers
* feat(api): Align notes
* feat(api): Don't mix quote styles in macros
* Apply suggestions from code review
Co-authored-by: Vadim Makeev <hi@pepelsbey.dev>
---------
Co-authored-by: Vadim Makeev <hi@pepelsbey.dev>
Here, we are using the {{domxref("window.prompt()", "window.prompt()")}} function, which prompts the user to answer a question via a popup dialog box and then stores the text they enter inside a given variable — in this case `name`. We then display a string that inserts the name into a generic greeting message.
You can click "Play" to see and edit the example in the MDN Playground.
86
+
Try clicking on the text label to see what happens.
86
87
87
-
Try clicking on this last version of the text label to see what happens (note also that you can find this demo on GitHub — see the [source code](https://github.com/mdn/learning-area/blob/main/javascript/introduction-to-js-1/what-is-js/javascript-label.html), or [run it live](https://mdn.github.io/learning-area/javascript/introduction-to-js-1/what-is-js/javascript-label.html))!
The **`HTMLTextAreaElement`** interface provides special properties and methods for manipulating the layout and presentation of {{HTMLElement("textarea")}} elements.
10
+
The **`HTMLTextAreaElement`** interface provides properties and methods for manipulating the layout and presentation of {{HTMLElement("textarea")}} elements.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis, arcu vitae adipiscing placerat, nisl lectus accumsan nisi, vitae iaculis sem neque vel lectus. Praesent tristique commodo lorem quis fringilla. Sed ac tellus eros. Sed consectetur eleifend felis vitae luctus. Praesent sagittis, est eget bibendum tincidunt, ligula diam tincidunt augue, a fermentum odio velit eget mi. Phasellus mattis, elit id fringilla semper, orci magna cursus ligula, non venenatis lacus augue sit amet dui. Pellentesque lacinia odio id nisi pulvinar commodo tempus at odio. Ut consectetur eros porttitor nunc mollis ultrices. Aenean porttitor, purus sollicitudin viverra auctor, neque erat blandit sapien, sit amet tincidunt massa mi ac nibh. Proin nibh sem, bibendum ut placerat nec, cursus et lacus. Phasellus vel augue turpis. Nunc eu mauris eu leo blandit mollis interdum eget lorem.
200
+
<textareaname="myTextArea"rows="10"cols="50">
201
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis, arcu vitae adipiscing placerat, nisl lectus accumsan nisi, vitae iaculis sem neque vel lectus. Praesent tristique commodo lorem quis fringilla. Sed ac tellus eros.
Copy file name to clipboardexpand all lines: files/en-us/web/api/window/confirm/index.md
+27-15
Original file line number
Diff line number
Diff line change
@@ -26,30 +26,41 @@ confirm(message)
26
26
27
27
### Return value
28
28
29
-
A boolean indicating whether OK (`true`) or Cancel (`false`) was
30
-
selected. If a browser is ignoring in-page dialogs, then the returned value is always
31
-
`false`.
29
+
A boolean indicating whether OK (`true`) or Cancel (`false`) was selected.
30
+
If a browser is ignoring in-page dialogs, then the returned value is always `false`.
32
31
33
32
## Examples
34
33
35
-
```js
36
-
if (window.confirm("Do you really want to leave?")) {
37
-
window.open("exit.html", "Thanks for Visiting!");
38
-
}
34
+
### Confirming before an action
35
+
36
+
The following example shows how to check the returned value of a confirmation dialog.
37
+
When the user clicks the OK button, we call {{domxref("window.open()")}}, and if the user clicks Cancel, we print some text to a {{htmlelement("pre")}} element.
prevent the user from accessing the rest of the program's interface until the dialog box
49
-
is closed. For this reason, you should not overuse any function that creates a dialog
50
-
box (or modal window). Regardless, there are good reasons to [avoid using dialog boxes for confirmation](https://alistapart.com/article/neveruseawarning/).
51
-
52
-
Alternatively {{HTMLElement("dialog")}} element can be used for confirmations.
61
+
Dialog boxes are modal windows — they prevent the user from accessing the rest of the program's interface until the dialog box is closed.
62
+
For this reason, you should not overuse any function that creates a dialog box or a modal window.
63
+
Alternatively, a {{HTMLElement("dialog")}} element can be used for confirmations.
53
64
54
65
## Specifications
55
66
@@ -64,3 +75,4 @@ Alternatively {{HTMLElement("dialog")}} element can be used for confirmations.
64
75
- {{HTMLElement("dialog")}} element
65
76
- {{domxref("window.alert()")}}
66
77
- {{domxref("window.prompt()")}}
78
+
-[Never Use a Warning When you Mean Undo](https://alistapart.com/article/neveruseawarning/) on A List Apart (2017)
`window.prompt()` instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.
12
12
13
-
Under some conditions — for example, when the user switches tabs — the browser may not actually display a dialog, or may not wait for the user to submit text or to cancel the dialog.
13
+
Under some conditions (when the user switches tabs, for example) the browser may not display a dialog, or may not wait for the user to submit text or to cancel the dialog.
14
14
15
15
## Syntax
16
16
@@ -34,48 +34,68 @@ A string containing the text entered by the user, or `null`.
34
34
35
35
## Examples
36
36
37
-
```js
38
-
let sign =prompt("What's your sign?");
37
+
### Using a prompt with a message and default value
38
+
39
+
The following example shows how to check the returned value of a prompt.
40
+
When the user clicks the OK button, text entered in the input field is returned.
41
+
If the user clicks OK without entering any text, an empty string is returned.
42
+
If the user clicks the Cancel button, this function returns `null`.
39
43
40
-
if (sign.toLowerCase() ==="scorpio") {
41
-
alert("Wow! I'm a Scorpio too!");
42
-
}
44
+
```html live-sample___prompt
45
+
<buttonid="signButton">Check star sign</button>
46
+
<preid="log"></pre>
47
+
```
43
48
44
-
// there are many ways to use the prompt feature
45
-
sign =window.prompt(); // open the blank prompt window
46
-
sign =prompt(); // open the blank prompt window
47
-
sign =window.prompt("Are you feeling lucky"); // open the window with Text "Are you feeling lucky"
48
-
sign =window.prompt("Are you feeling lucky", "sure"); // open the window with Text "Are you feeling lucky" and default value "sure"
The above prompt appears as follows (in Chrome on macOS):
72
+
There are multiple ways to use a prompt, using `prompt`, `window.prompt`, and providing a message and default values:
56
73
57
-

74
+
```js
75
+
// open a blank prompt window
76
+
sign =prompt();
77
+
// open a blank prompt window
78
+
sign =window.prompt();
79
+
// open a prompt with the text "Are you feeling lucky"
80
+
sign =window.prompt("Are you feeling lucky");
81
+
// open a prompt with the text "Are you feeling lucky" and "sure" as the default value
82
+
sign =prompt("Are you feeling lucky", "sure");
83
+
```
58
84
59
85
## Notes
60
86
61
-
A prompt dialog contains a single-line textbox, a Cancel button, and an OK button, and
62
-
returns the (possibly empty) text the user entered into that textbox.
87
+
Dialog boxes are modal windows — they prevent the user from accessing the rest of the program's interface until the dialog box is closed.
88
+
For this reason, you should not overuse any function that creates a dialog box or a modal window.
89
+
Alternatively, a {{HTMLElement("dialog")}} element can be used for confirmations.
63
90
64
-
Please note that result is a string. That means you should sometimes cast the value
65
-
given by the user. For example, if their answer should be a Number, you should cast the
66
-
value to Number.
91
+
A prompt dialog contains a single-line textbox, a Cancel button, and an OK button, and returns the (possibly empty) text the user entered into that textbox.
92
+
The result is a string, which means you should sometimes cast the value given by the user.
93
+
For example, if their answer should be a Number, you should cast the value to Number.
67
94
68
95
```js
69
96
constaNumber=Number(window.prompt("Type a number", ""));
70
97
```
71
98
72
-
Dialog boxes are modal windows; they
73
-
prevent the user from accessing the rest of the program's interface until the dialog box
74
-
is closed. For this reason, you should not overuse any function that creates a dialog
75
-
box (or modal window).
76
-
77
-
Alternatively {{HTMLElement("dialog")}} element can be used to take user inputs.
78
-
79
99
## Specifications
80
100
81
101
{{Specifications}}
@@ -89,3 +109,4 @@ Alternatively {{HTMLElement("dialog")}} element can be used to take user inputs.
89
109
- {{HTMLElement("dialog")}} element
90
110
- {{domxref("window.alert", "alert")}}
91
111
- {{domxref("window.confirm", "confirm")}}
112
+
-[Never Use a Warning When you Mean Undo](https://alistapart.com/article/neveruseawarning/) on A List Apart (2017)
0 commit comments