-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathalert-prompt-confirm.html
More file actions
1071 lines (1045 loc) · 44 KB
/
Copy pathalert-prompt-confirm.html
File metadata and controls
1071 lines (1045 loc) · 44 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<script>
window.currentUser = null;
</script>
<script>
window.rateUsdToNative = 1;
</script>
<title itemprop="name">Interaction: alert, prompt, confirm</title>
<link href="pack/styles.93b05845f313a968119b.css" rel="stylesheet" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1.0"
/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<script>
if (window.devicePixelRatio > 1)
document.cookie =
"pixelRatio=" +
window.devicePixelRatio +
";path=/;expires=Tue, 19 Jan 2038 03:14:07 GMT";
</script>
<link
href="https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic"
rel="stylesheet"
/>
<link
rel="apple-touch-icon-precomposed"
<link rel="canonical" href="alert-prompt-confirm.html" />
<link rel="icon" href="img/favicon/favicon.png" />
<meta property="og:title" content="Interaction: alert, prompt, confirm" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="fb:admins" content="100001562528165" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Interaction: alert, prompt, confirm" />
<link rel="prev" href="types.html" />
<link rel="next" href="type-conversions.html" />
<script>
window.GA_ID = "UA-2056213-15";
</script>
<script>
window.YANDEX_METRIKA_ID = 32184394;
</script>
<script>
(window.GoogleAnalyticsObject = "ga"),
(window.ga = function () {
window.ga.q.push(arguments);
}),
(window.ga.q = []),
(window.ga.l = Date.now()),
ga("create", GA_ID, "auto"),
window.GTM_ID && ga("require", GTM_ID),
window.currentUser
? ga("set", "&uid", currentUser.id)
: ga("set", "anonymizeIp", !0),
window.addEventListener("error", function (e) {
var r = (e.filename || e.errorUrl) + ": " + (e.lineno || e.errorLine),
n = e.stack || (e.error && e.error.stack);
ga("send", "exception", {
exDescription: e.message + " " + r + " " + n,
exFatal: !0,
});
});
</script>
<script src="https://www.google-analytics.com/analytics.js" async></script>
<script>
ga("send", "pageview");
</script>
<script>
(window.metrika = { reachGoal: function () {} }),
(window.yandex_metrika_callbacks = [
function () {
try {
(window.metrika = new Ya.Metrika({
id: YANDEX_METRIKA_ID,
webvisor: !0,
clickmap: !0,
params: { user: window.currentUser && window.currentUser.id },
})),
metrika.trackLinks({ delay: 150 }),
window.addEventListener("error", function (r) {
window.metrika.reachGoal("JSERROR", {
src:
(r.filename || r.errorUrl) +
": " +
(r.lineno || r.errorLine),
stack: r.stack || (r.error && r.error.stack),
message: r.message,
});
});
} catch (r) {}
},
]);
</script>
<script src="https://mc.yandex.ru/metrika/watch.js" async></script>
<script>
window.RECAPTCHA_ID = "6LfmLAEVAAAAAJMykMnf7aY8nkyTRmYi2ynx51R1";
</script>
<script src="pack/init.810c8da2b4eed830d2da.js"></script>
<script src="pack/head.282ce219a840c583fa6d.js" defer></script>
<meta property="og:title" content="Interaction: alert, prompt, confirm" />
<script src="pack/tutorial.dd771765a083d97231c4.js" defer></script>
<script src="pack/footer.ce9ac5e9c03240648299.js" defer></script>
</head>
<body class="no-icons">
<div class="sitetoolbar__content">
<div class="sitetoolbar__lang-switcher">
<button class="sitetoolbar__dropdown-button" data-dropdown-toggler>
EN
</button>
<div class="sitetoolbar__dropdown-wrap">
<div class="sitetoolbar__dropdown-body">
<div class="sitetoolbar__lang-switcher-body">
<div class="supported-langs supported-langs_toolbar">
<div class="supported-langs__container">
<ul class="supported-langs__list" style="height: 200px">
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://ar.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">AR</span
><span>عربي</span></a
>
</li>
<li
class="
supported-langs__item supported-langs__item_current
"
>
<a
class="supported-langs__link"
href="alert-prompt-confirm.html"
><span class="supported-langs__brief">EN</span
><span>English</span></a
>
</li>
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://es.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">ES</span
><span>Español</span></a
>
</li>
alert-prompt-confirm"
><span class="supported-langs__brief">FR</span
><span>Français</span></a
>
</li>
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://it.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">IT</span
><span>Italiano</span></a
>
</li>
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://ja.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">JA</span
><span>日本語</span></a
>
</li>
</ul>
<ul class="supported-langs__list" style="height: 128px">
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://ko.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">KO</span
><span>한국어</span></a
>
</li>
<li class="supported-langs__item">
<a
class="supported-langs__link"
href=alert-prompt-confirm"
><span class="supported-langs__brief">RU</span
><span>Русский</span></a
>
</li>
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://tr.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">TR</span
><span>Türkçe</span></a
>
</li>
<li class="supported-langs__item">
<a
class="supported-langs__link"
href="https://zh.javascript.info/alert-prompt-confirm"
><span class="supported-langs__brief">ZH</span
><span>简体中文</span></a
>
</li>
</ul>
</div>
<div class="supported-langs__text">
<p>
We want to make this open-source project available for
people all around the world.
</p>
<p>
<a href="translate.html">Help to translate</a>
the content of this tutorial to your language!
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sitetoolbar__logo-wrap">
<a
class="sitetoolbar__link sitetoolbar__link_logo"
href="index.html"
><img
class="sitetoolbar__logo sitetoolbar__logo_normal"
src="img/sitetoolbar__logo_en.svg"
width="200"
alt=""
role="presentation"
/><img
class="sitetoolbar__logo sitetoolbar__logo_small"
src="img/sitetoolbar__logo_small_en.svg"
width="70"
alt=""
role="presentation"
/>
<script>
Array.prototype.forEach.call(
document.querySelectorAll("img.sitetoolbar__logo"),
function (e) {
let t = document.createElement("object");
(t.type = "image/svg+xml"),
(t.className = e.className),
(t.style.cssText = "left:0;top:0;position:absolute"),
(t.onload = function () {
(t.onload = null), (e.style.visibility = "hidden");
}),
(t.data = e.src),
e.parentNode.insertBefore(t, e);
}
);
</script></a
>
</div>
</div>
</div>
<div class="tablet-menu__line">
<div class="tablet-menu__content">
<div class="share-icons">
<span class="share-icons__title">Share</span
>2Falert-prompt-confirm"
rel="nofollow"
></a
>2Falert-prompt-confirm
<select
class="
tablet-menu__nav
input-select input-select input-select_small
"
onchange="if(this.value) window.location.href=this.value"
>
<option value="https://ar.javascript.info/alert-prompt-confirm">
عربي
</option>
<option
value="https://javascript.info/alert-prompt-confirm"
selected
>
English
</option>
<option value="https://es.javascript.info/alert-prompt-confirm">
Español
</option>
<option value="https://fr.javascript.info/alert-prompt-confirm">
Français
</option>
<option value="https://it.javascript.info/alert-prompt-confirm">
Italiano
</option>
<option value="https://ja.javascript.info/alert-prompt-confirm">
日本語
</option>
<option value="https://ko.javascript.info/alert-prompt-confirm">
한국어
</option>
<option
value=alert-prompt-confirm"
>
Русский
</option>
<option value="https://tr.javascript.info/alert-prompt-confirm">
Türkçe
</option>
<option value="https://zh.javascript.info/alert-prompt-confirm">
简体中文
</option>
</select>
</div>
</div>
</div>
<progress
class="tutorial-progress"
data-sticky
value="10"
max="92"
data-tooltip="Lesson 10 of 92"
></progress>
</div>
<div class="page page_sidebar_on page_inner_padding">
<script>
if (localStorage.noSidebar) {
document.querySelector(".page").classList.remove("page_sidebar_on");
let e = document.querySelector(".page-wrapper");
e && e.classList.remove("page-wrapper_sidebar_on");
}
setTimeout(function () {
document
.querySelector(".page")
.classList.add("page_sidebar-animation-on");
});
</script>
<div class="page__inner">
<main class="main main_width-limit">
<header class="main__header">
<div class="main__header-inner">
<div class="main__header-group">
<ol class="breadcrumbs">
<li class="breadcrumbs__item breadcrumbs__item_home">
<a class="breadcrumbs__link" href="index.html"
><span class="breadcrumbs__hidden-text"
>Tutorial</span
></a
>
</li>
<li class="breadcrumbs__item" id="breadcrumb-1">
<a class="breadcrumbs__link" href="js.html"
><span>The JavaScript language</span></a
>
</li>
<li class="breadcrumbs__item" id="breadcrumb-2">
<a class="breadcrumbs__link" href="first-steps.html"
><span>JavaScript Fundamentals</span></a
>
</li>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Tutorial",
"item": "https://javascript.info/"
},
{
"@type": "ListItem",
"position": 2,
"name": "The JavaScript language",
"item": "https://javascript.info/js"
},
{
"@type": "ListItem",
"position": 3,
"name": "JavaScript Fundamentals",
"item": "https://javascript.info/first-steps"
}
]
}
</script>
</ol>
<div
class="updated-at"
data-tooltip="Last updated at 8th September 2020"
>
<div class="updated-at__content">8th September 2020</div>
</div>
</div>
<h1 class="main__header-title">
Interaction: alert, prompt, confirm
</h1>
</div>
</header>
<div class="content">
<article
class="formatted"
itemscope
itemtype="http://schema.org/TechArticle"
>
<meta
itemprop="name"
content="Interaction: alert, prompt, confirm"
/>
<div
itemprop="author"
itemscope
itemtype="http://schema.org/Person"
>
<meta itemprop="email" content="iliakan@gmail.com" /><meta
itemprop="name"
content="Ilya Kantor"
/>
</div>
<div itemprop="articleBody">
<p>
As we’ll be using the browser as our demo environment, let’s
see a couple of functions to interact with the user:
<code>alert</code>, <code>prompt</code> and
<code>confirm</code>.
</p>
<h2>
<a
class="main__anchor"
name="alert"
href="alert-prompt-confirm.html#alert"
>alert</a
>
</h2>
<p>
This one we’ve seen already. It shows a message and waits
for the user to press “OK”.
</p>
<p>For example:</p>
<div id="cvcmn2ffbe" data-trusted="1" class="code-example">
<div class="codebox code-example__codebox">
<div class="toolbar codebox__toolbar">
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="run"
data-action="run"
class="toolbar__button toolbar__button_run"
></a>
</div>
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="open in sandbox"
target="_blank"
data-action="edit"
class="toolbar__button toolbar__button_edit"
></a>
</div>
</div>
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>alert("Hello");</code></pre>
</div>
</div>
</div>
<p>
The mini-window with the message is called a
<em>modal window</em>. The word “modal” means that the
visitor can’t interact with the rest of the page, press
other buttons, etc, until they have dealt with the window.
In this case – until they press “OK”.
</p>
<h2>
<a
class="main__anchor"
name="prompt"
href="alert-prompt-confirm.html#prompt"
>prompt</a
>
</h2>
<p>The function <code>prompt</code> accepts two arguments:</p>
<div id="02x61kn3jy" data-trusted="1" class="code-example">
<div class="codebox code-example__codebox">
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>result = prompt(title, [default]);</code></pre>
</div>
</div>
</div>
<p>
It shows a modal window with a text message, an input field
for the visitor, and the buttons OK/Cancel.
</p>
<dl>
<dt><code>title</code></dt>
<dd>The text to show the visitor.</dd>
<dt><code>default</code></dt>
<dd>
An optional second parameter, the initial value for the
input field.
</dd>
</dl>
<div class="important important_smart">
<div class="important__header">
<span class="important__type"
>The square brackets in syntax <code>[...]</code></span
>
</div>
<div class="important__content">
<p>
The square brackets around <code>default</code> in the
syntax above denote that the parameter is optional, not
required.
</p>
</div>
</div>
<p>
The visitor can type something in the prompt input field and
press OK. Then we get that text in the <code>result</code>.
Or they can cancel the input by pressing Cancel or hitting
the <kbd class="shortcut">Esc</kbd> key, then we get
<code>null</code> as the <code>result</code>.
</p>
<p>
The call to <code>prompt</code> returns the text from the
input field or <code>null</code> if the input was canceled.
</p>
<p>For instance:</p>
<div id="glsvx1ydfn" data-trusted="1" class="code-example">
<div class="codebox code-example__codebox">
<div class="toolbar codebox__toolbar">
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="run"
data-action="run"
class="toolbar__button toolbar__button_run"
></a>
</div>
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="open in sandbox"
target="_blank"
data-action="edit"
class="toolbar__button toolbar__button_edit"
></a>
</div>
</div>
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>let age = prompt('How old are you?', 100);
alert(`You are ${age} years old!`); // You are 100 years old!</code></pre>
</div>
</div>
</div>
<div class="important important_warn">
<div class="important__header">
<span class="important__type"
>In IE: always supply a <code>default</code></span
>
</div>
<div class="important__content">
<p>
The second parameter is optional, but if we don’t supply
it, Internet Explorer will insert the text
<code>"undefined"</code> into the prompt.
</p>
<p>Run this code in Internet Explorer to see:</p>
<div
id="j8dyokxvjt"
data-trusted="1"
class="code-example"
>
<div class="codebox code-example__codebox">
<div class="toolbar codebox__toolbar">
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="run"
data-action="run"
class="toolbar__button toolbar__button_run"
></a>
</div>
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="open in sandbox"
target="_blank"
data-action="edit"
class="toolbar__button toolbar__button_edit"
></a>
</div>
</div>
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>let test = prompt("Test");</code></pre>
</div>
</div>
</div>
<p>
So, for prompts to look good in IE, we recommend always
providing the second argument:
</p>
<div
id="iy8yk12a8m"
data-trusted="1"
class="code-example"
>
<div class="codebox code-example__codebox">
<div class="toolbar codebox__toolbar">
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="run"
data-action="run"
class="toolbar__button toolbar__button_run"
></a>
</div>
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="open in sandbox"
target="_blank"
data-action="edit"
class="toolbar__button toolbar__button_edit"
></a>
</div>
</div>
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>let test = prompt("Test", ''); // <-- for IE</code></pre>
</div>
</div>
</div>
</div>
</div>
<h2>
<a
class="main__anchor"
name="confirm"
href="alert-prompt-confirm.html#confirm"
>confirm</a
>
</h2>
<p>The syntax:</p>
<div id="nz54lwdezm" data-trusted="1" class="code-example">
<div class="codebox code-example__codebox">
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>result = confirm(question);</code></pre>
</div>
</div>
</div>
<p>
The function <code>confirm</code> shows a modal window with
a <code>question</code> and two buttons: OK and Cancel.
</p>
<p>
The result is <code>true</code> if OK is pressed and
<code>false</code> otherwise.
</p>
<p>For example:</p>
<div id="hbsnhl62bc" data-trusted="1" class="code-example">
<div class="codebox code-example__codebox">
<div class="toolbar codebox__toolbar">
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="run"
data-action="run"
class="toolbar__button toolbar__button_run"
></a>
</div>
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="open in sandbox"
target="_blank"
data-action="edit"
class="toolbar__button toolbar__button_edit"
></a>
</div>
</div>
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>let isBoss = confirm("Are you the boss?");
alert( isBoss ); // true if OK is pressed</code></pre>
</div>
</div>
</div>
<h2>
<a
class="main__anchor"
name="summary"
href="alert-prompt-confirm.html#summary"
>Summary</a
>
</h2>
<p>
We covered 3 browser-specific functions to interact with
visitors:
</p>
<dl>
<dt><code>alert</code></dt>
<dd>shows a message.</dd>
<dt><code>prompt</code></dt>
<dd>
shows a message asking the user to input text. It returns
the text or, if Cancel button or
<kbd class="shortcut">Esc</kbd> is clicked,
<code>null</code>.
</dd>
<dt><code>confirm</code></dt>
<dd>
shows a message and waits for the user to press “OK” or
“Cancel”. It returns <code>true</code> for OK and
<code>false</code> for Cancel/<kbd class="shortcut"
>Esc</kbd
>.
</dd>
</dl>
<p>
All these methods are modal: they pause script execution and
don’t allow the visitor to interact with the rest of the
page until the window has been dismissed.
</p>
<p>
There are two limitations shared by all the methods above:
</p>
<ol>
<li>
The exact location of the modal window is determined by
the browser. Usually, it’s in the center.
</li>
<li>
The exact look of the window also depends on the browser.
We can’t modify it.
</li>
</ol>
<p>
That is the price for simplicity. There are other ways to
show nicer windows and richer interaction with the visitor,
but if “bells and whistles” do not matter much, these
methods work just fine.
</p>
</div>
</article>
<div class="tasks formatted">
<h2 class="tasks__title" id="tasks">
<a
class="
tasks__title-anchor
main__anchor main__anchor main__anchor_noicon
"
href="alert-prompt-confirm.html#tasks"
>Tasks</a
>
</h2>
<div class="task tasks__task">
<div class="task__header">
<div class="task__title-wrap">
<h3 class="task__title">
<a
class="main__anchor"
href="alert-prompt-confirm.html#a-simple-page"
name="a-simple-page"
>A simple page</a
>
</h3>
<a
class="task__open-link"
href="task/simple-page.html"
target="_blank"
></a>
</div>
<div class="task__header-note">
<span
class="task__importance"
title="How important is the task, from 1 to 5"
>importance: 4</span
>
</div>
<div class="task__content">
<div class="task__formatted">
<p>
Create a web-page that asks for a name and outputs it.
</p>
<p>
<a
href="alert-prompt-confirm.html#"
onclick="event.preventDefault(); runDemo(this)"
>Run the demo</a
>
</p>
</div>
<button class="task__solution" type="button">
solution
</button>
<div class="task__answer">
<div class="task__answer-content">
<div class="formatted">
<p>JavaScript-code:</p>
<div
id="r3omhz0oi9"
data-trusted="1"
class="code-example"
data-demo="1"
>
<div class="codebox code-example__codebox">
<div class="toolbar codebox__toolbar">
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="run"
data-action="run"
class="
toolbar__button toolbar__button_run
"
></a>
</div>
<div class="toolbar__tool">
<a
href="alert-prompt-confirm.html#"
title="open in sandbox"
target="_blank"
data-action="edit"
class="
toolbar__button toolbar__button_edit
"
></a>
</div>
</div>
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-javascript"
><code>let name = prompt("What is your name?", "");
alert(name);</code></pre>
</div>
</div>
</div>
<p>The full page:</p>
<div
id="8omiu25dxe"
data-trusted="1"
class="code-example"
>
<div class="codebox code-example__codebox">
<div class="codebox__code" data-code="1">
<pre
class="line-numbers language-markup"
><code><!DOCTYPE html>
<html>
<body>
<script>
'use strict';
let name = prompt("What is your name?", "");
alert(name);
</script>
</body>
</html></code></pre>
</div>
</div>
</div>
</div>
</div>
<button
class="close-button task__answer-close"
type="button"
title="close"
></button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="page__nav-wrap">
<a
class="page__nav page__nav_prev"
href="types.html"
data-tooltip="Data types"
><span class="page__nav-text"
><span class="page__nav-text-shortcut"></span></span
><span class="page__nav-text-alternate"
>Previous lesson</span
></a
><a
class="page__nav page__nav_next"
href="type-conversions.html"
data-tooltip="Type Conversions"
><span class="page__nav-text"
><span class="page__nav-text-shortcut"></span></span
><span class="page__nav-text-alternate">Next lesson</span></a
>
</div>
<div class="article-tablet-foot tablet-only">
<div class="article-tablet-foot__layout">
<div class="share-icons">
<span class="share-icons__title">Share</span
><a
class="share share_tw"
s%3A%2F%2Fjavascript.info%2Falert-prompt-confirm"
rel="nofollow"
></a
><a
class="share share_fb"
href="https://www.facebook.com/sharer/sharer.php?s=100&p%5Burl%5D=https%3A%2F%2Fjavascript.info%2Falert-prompt-confirm"
rel="nofollow"
></a>
</div>
<div class="article-tablet-foot__map">
<a
class="map"
href="tutorial/map.html"
data-action="tutorial-map"
><span class="map__text">Tutorial map</span></a
>
</div>
</div>
</div>
<a href="alert-prompt-confirm.html name="comments"
>Comments</a
>
</h2>
<div class="comments__read-before">
<span class="comments__read-before-link"
>read this before commenting…</span
>
<div class="comments__read-before-popup">
<div class="comments__read-before-popup-i">
<ul>
<li>
If you have suggestions what to improve - please
or a pull request instead of commenting.
</li>
<li>
If you can't understand something in the article –
please elaborate.
</li>
<li>
To insert few words of code, use the
<code><code></code> tag, for several lines –
wrap them in <code><pre></code> tag, for more
than 10 lines – use a sandbox (<a
href="https://plnkr.co/edit/?p=preview"
>plnkr</a
>, <a href="https://jsbin.com">jsbin</a>,
<a href="http://codepen.io">codepen</a>…)
</li>
</ul>
</div>
</div>
</div>
</div>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
if (!this.page) this.page = {};
Object.assign(this.page, {
url: "https:\/\/javascript.info\/alert-prompt-confirm",
identifier: "\/alert-prompt-confirm",
});
};
</script>
<script>
var disqus_shortname = "javascriptinfo";
</script>
<script>
var disqus_enabled = true;
</script>
</div>
</main>
</div>
<a class="sidebar__link" href="first-steps.html"
>JavaScript Fundamentals</a