|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Using Message Boxes
Although browsers support most JScript features, the new features that target the .NET Framework, class-based objects, data types, enumerations, conditional compilation directives, and the const statement, are supported only on the server-side. Consequently, you should use these features exclusively in server-side scripts. For more information, see.
Whenever a script is intended to run in a browser (client-side), experienced developers include code that detects the version of the script engine. After the script detects the engine version, it can redirect the browser to a page with script that is compatible with the browser's script engine. For more information, see.
JScript uses the alert, confirm, and prompt message boxes of the browser to obtain input from your user. The boxes are methods of the window object. Because the window object is at the top of the object hierarchy, you do not actually need to use the full name (for example, window.alert()) of any of these message boxes, but it is a good idea to do so because it helps you remember to which object they belong.
Alert Message Box
The alert method has one argument, the string of text that you want to display in the alert message box. The string is not HTML. The message box provides an OK button to close the message box and is modal, that is, the user must close the message box before continuing.
window.alert("Welcome! Press OK to continue.");
Confirm Message Box
The confirm message box, which includes OK and Cancel buttons, poses a question with two possible outcomes. The confirm method returns either true or false. This message box is also modal: the user must respond to it (click a button), and thereby close it, before proceeding.
var truthBeTold = window.confirm("Click OK to continue. Click Cancel to stop."); if (truthBeTold) window.alert("Welcome to our Web page!"); else window.alert("Bye for now!");
Prompt Message Box
The prompt message box, which includes OK and Cancel buttons, provides a text field that accepts text in response to a prompt. If you provide a second string argument, the prompt message box displays the second string in the text field as the default response. Otherwise, the default text is "undefined".
Like the alert and confirm methods, prompt displays a modal message box. The user must close it before continuing.
var theResponse = window.prompt("Welcome?","Enter your name here."); document.write("Welcome "+theResponse+".<BR>");