Monday, July 21, 2008

How to Create a Confirm Box in JavaScript

Confirm boxes are a convenient way to give a web visitor a simple choice: to accept or to decline something. They are displayed using the JavaScript confirm function।

Create a Confirm Box in JavaScript

Step1
Write the code to call the confirm function, following the example below. The result from confirm is placed in the variable ok so it can be tested in subsequent logic.
ok = confirm("Do you really want to do this?");
Step2
Concatenate strings using the "+" operator to display a message made up of several parts, as follows:
ok = confirm ("Are you really " + age + " years old?");
  • f you don't need to give the user the option to Cancel, consider using the alert function instead.
  • Instead of storing the result from confirm in a variable and testing it, you can test it directly within an ifstatement with a construct such as if (confirm(...) { }.
  • Technically the confirm function is a method on the JavaScript window object, and thus could be written window.confirm, but since JavaScript on web pages is executed within the context of the window object anyway, this is not necessary.

No comments: