JavaScript System Dialogs alert(),confirm(),prompt()

The browser is capable of invoking system dialogs to display to the user through the alert(),
confirm(), and prompt() methods. These dialogs are not related to the web page being displayed
in the browser and do not contain HTML. Their appearance is determined by operating system and/
or browser settings rather than CSS. Additionally, each of these dialogs is synchronous and modal,
meaning code execution stops when a dialog is displayed, and resumes
after it has been dismissed.

The alert() method has been used throughout this book. It simply
accepts a string to display to the user. When alert() is called, a
system message box displays the specifi ed text to the user, followed by
a single OK button.

Alert dialogs are typically used when users must be made aware of something that they have no
control over, such as an error. A user’s only choice is to dismiss the dialog after reading the message.
The second type of dialog is invoked by calling confirm(). A confi rm dialog looks similar to an
alert dialog in that it displays a message to the user. The main difference between the two is the

To determine if the user clicked OK or Cancel, the confirm() method
returns a Boolean value: true if OK was clicked, or false if Cancel
was clicked or the dialog box was closed by clicking the X in the
corner. Typical usage of a confi rm dialog looks like this:

if (confirm(“Are you sure?”)) {
alert(“I’m so glad you’re sure! “);
} else {
alert(“I’m sorry to hear you’re not sure. “);
}

If the OK button is clicked, prompt() returns the value in the text box; if Cancel is clicked or the
dialog is otherwise closed without clicking OK, the function returns null. Here’s an example:

var result = prompt(“What is your name? “, “”);
if (result !== null) {
alert(“Welcome, “ + result);
}
Related Posts:
  • Validate Email address   Checking Email  Web sites often use email addresses as usernames because they are guaranteed to be unique, as long as they are valid. In addition, the organizations can use the email addresses to communi… Read More
  • Creating a Cookie-Javascript To make life easier for ourselves, we'll write a function that allows us to create a new cookie and set certain of its attributes with more ease. We'll look at the code first and create an example using it shortly. fun… Read More
  • javascript Date, Time Best seo practices How to promoting webpages Top seo tips top 10 e-commerce tips Additional seo tips Advanced seo tips Mobile seo tips Javascript Date The methods getDate(), getDay(), getMonth(), and g… Read More
  • Include Inline JavaScript code To include inline JavaScript code, place JavaScript code inside the <script> element directly, as follows: <script type=”text/javascript”> function sayHi(){ alert(“Hi!”); } </script> When using inline Java… Read More
  • JavaScript Timers setTimeout() and setInterval() allow you to register a functionto be invoked once or repeatedly after a specified amountof time has elapsed. These are important global functions ofclient-side JavaScript, and are therefore de… Read More
  • Detecting JavaScript and Cookie Compatibility You may be expecting a huge dump of code to see if JavaScript and cookies are enabled. There ’ s no way that you ’ d want to go through with something like that at this point in the project, so the following minimalist code … Read More
  • Prototypes Every Java-Script object has a second JavaScript object (or null, but thisis rare) associated with it. This second object is known as aprototype, and the first object inherits properties from theprototype.All objects created… Read More
  • Javascript Array Creating an Object We have already seen an example of an Array object being created. To create an Array object, we used the JavaScript statement var myArray = new Array(); So how is this statement made up? The first ha… Read More
  • Validate a Text Field No web developers want their Ajax applications to hit the network with requests if the users leave necessary text fields blank. Thus, checking that input elements of type text and the large boxes called textareas in … Read More
  • Javascript cookie string We can set a cookie by setting document.cookie to a cookie string.  The following code will set a cookie with the UserName set as Paul,  and an expiration date of 28 December 2010. <html> <head> <… Read More
  • JavaScript Interview Questions JavaScript Interview Questions What is the difference between == and === ? The == checks for value equality, but === checks for both type and value. difference between innerHTML and append() in JavaScript? InnerHTML is… Read More
  • Javascript Calendar script Javascript Calendar script <style type="text/css"> .main { width:200px; border:1px solid black; } .month { background-color:black; font:bold 12px verdana; color:white; } .daysofweek { background-color:gray; fo… Read More
  • javascript Variable Scope Microsoft JScript has two scopes: global and local. If you declare a variable outside of any function definition, it is a global variable, and its value is accessible and modifiable throughout your program. If you declar… Read More
  • Array Indexing-javascript Array Indexing Arrays in JScript are sparse. That is, if you have an array with three elements that are numbered 0, 1, and 2, you can create element 50 without worrying about elements 3 through 49. If the array has an … Read More
  • PHP and Javascript Variables Variables To define a variable in PHP, you’d write:// PHP$n = 1;The equivalent in JavaScript is:// JavaScriptvar n = 1; There’s no dollar sign, just the name of the variable.  Like in PHP, you don’t define variable… Read More