JavaScript Alerting and Prompting the User
syllabus | schedule | assignments | Web Resources | ARTC courses
[Intro]     [Tags]     [CSSStyles]     [Lists]     [Links]     [ImageTypes]     [Optimization]    [GlobalCSS]     [CSSBackgrounds]
[Tables]     [Slices]     [IR Rollovers]     [Animated GIFs]     [JS Rollovers]     [CSS Margins]     [Windows]     [Video]     [Metatags]     [Imagemaps]     [FormInput]     [FormAtttributes]    [AdvancedForms]    [WhatisJavaScript]    [Windows2]    [Windows3]    [BrowserInfo]    [StatusBar]    [Alerts]    [Randomness]    [Frames]    [iFrames]    [Frames2]   
Alert Box - used to display a message in a pop-up window.

JavaScript has a function called alert() which pops up a window with the message you pass as parameter. The simplest example using this function is to display a message when your page loads.

EX 1: <A HREF="JavaScript: alert('your message here.');">click here</a>

JavaScript: tells the browser that it should execute the given JavaScript statement when the link is clicked.

Test this code: click here. Notice the status bar when you test this code.

 

Prompting User - The following examples use the alert box to prompt the user to respond.

EX 1: <sample>

<head>
<script>

ans = prompt( "Are you sure you want to do that?" );

if (ans) {
alert( "You said " + ans );
} else {
alert( "You refused to answer" );
}

</script>
</head>

EX 2: <sample>

<head>
<script>

ans = prompt( "Are you sure you want to do that?" );

if ( ans == "yes" ) {
//if ans is same as yes, then do this...

location.replace( "http://bgsu.edu" );

} else if ( ans == "no" ) { //else if ans is same as no, then do this...

location.replace( "http://syr.edu" );

} else { //in all other cases (not yes or no) - do this...

document.write( "I don't understand: " + ans );

}

</script>
</head>

<ref: javascriptworld.com>