3 - User Interaction - Alert, Prompt and Confirm - JavaScript Tutorial
3 - User Interaction - Alert, Prompt and Confirm - JavaScript Tutorial
JavaScript Tutorial
Home Tutorial JavaScript: from the Ground to Closures First Steps
Browser Developer's Tools Operators Tutorial
Like
Setup your environment Hello, World! Variables and statements Browser Developer's Tools User interaction: alert, prompt and confirm Operators and constructs
a l e r t
The syntax: a l e r t ( m e s s a g e ) a l e r toutputs a window with the message and stops execution until visitor presses OK button. The message window is called a modal window. The word modal means that page interface is suspended until OK is clicked. A visitor cant do anything until he clicks OK.
Run!
Functions: declarations and expressions Mastering data types Four scents of "this" Type detection Function arguments Static variables and methods Scopes and Closures Decorators
1 a l e r t ( " T e s tm e " ) ;
Document and Events Object Oriented Programming Timing Frames and windows Regular expressions in JavaScript Advanced and Extra stuff
p r o m p t
This method accepts two arguments: r e s u l t=p r o m p t ( t e x t [ ,d e f a u l t ] ) ; It outputs a modal window with the t e x t , OK/CANCEL buttons and input field. p r o m p treturns either a string (maybe empty) or n u l l . The result depends on users action. There are three options: 1. If user types something into field and presses OK, then user text is the result. 2. If user types nothing, but presses OK, then the result is d e f a u l t . 3. If user presses CANCEL (or keyboard Escape), then result is n u l l . As with a l e r t , the window is modal, user cant do anything until he presses one of two buttons (or Escape which is same as CANCEL).
Run!
1 p r o m p t ( " T e s t " )
c o n f i r m
The syntax: r e s u l t=c o n f i r m ( m e s s a g e ) c o n f i r moutputs the m e s s a g ewith two buttons: OK and CANCEL. The result is t r u e / f a l s e , for OK/CANCEL. For example:
http://javascript.info/tutorial/user-interaction-alert-prompt-and-confirm
1/2
1/21/2014
Run!
1 v a rr e s u l t=c o n f i r m ( " S h o u l dIs a yh e l l o ? " ) 2 3 a l e r t ( r e s u l t ) Note that you cant style, decorate or change screen position of basic modal windows. Thats the main drawback of basic UI functions. But all these functions are simple and require no additional code. Thats why people still use them.
Create a page which asks for a name and alerts it. The page should work as following: tutorial/intro/basic.html .
Open solution
Summary
a l e r toutputs a message. p r o m p toutputs a message and waits for user input, then returns the value or n u l lif ESC is pressed. c o n f i r moutputs a message and awaits until user presses ok or cancel. The returned value is t r u e / f a l s e Browser Developer's Tools Operators
The content of this site is available under the terms of CC BY-NC-SA. Ilya Kantor, 2011.
"JavaScript is a registered trademark of Oracle corp. This site is not affiliated with Oracle corp.
http://javascript.info/tutorial/user-interaction-alert-prompt-and-confirm
2/2