1/21/2014

User interaction: alert, prompt and confirm | JavaScript Tutorial
Log in

JavaScript Tutorial
Home » Tutorial » JavaScript: from the Ground to Closures » First Steps
‹ Browser Developer's Tools Operators › Tutorial

JavaScript: from the Ground to Closures
JavaScript: from the Ground to Closures Javascript and related technologies First Steps
First Steps

User interaction: alert, prompt and confirm
Ilya Kantor

1. a l e r t 2. p r o m p t 3. c o n f i r m 4. Summary The basic UI operations are a l e r t ,p r o m p tand c o n f i r m .

Like

2

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 can’t 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 user’s 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 can’t do anything until he presses one of two buttons (or Escape which is same as CANCEL).
Run!

Donate Donate to this project

1 v a ry e a r s=p r o m p t ( ' H o wo l da r ey o u ? ' ,1 0 0 ) 2 a l e r t ( ' Y o ua r e'+y e a r s+'y e a r so l d ! ' )

Always set second argument
Always set the default value in p r o m p t , otherwise IE will insert " u n d e f i n e d "into dialog: Try this in IE:
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

But all these functions are simple and require no additional code. "JavaScript™ is a registered trademark of Oracle corp. The page should work as following: tutorial/intro/basic. This site is not affiliated with Oracle corp.html .1/21/2014 Run! User interaction: alert. © Ilya Kantor. then returns the value or n u l lif ESC is pressed. 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. p r o m p toutputs a message and waits for user input. Open solution Summary a l e r toutputs a message. prompt and confirm | JavaScript Tutorial 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 can’t style. That’s why people still use them. Create a page which asks for a name and alerts it. http://javascript. That’s the main drawback of basic UI functions.info/tutorial/user-interaction-alert-prompt-and-confirm 2/2 . decorate or change screen position of basic modal windows. c o n f i r moutputs a message and awaits until user presses ok or cancel. 2011.