11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note <strong>The</strong> typical rendering of the alert includes an icon indicating a warning, regardless of the<br />

meaning of the message being presented.<br />

<strong>The</strong> basic syntax for alert() is<br />

window.alert(string);<br />

or for shorthand,<br />

alert(string);<br />

as the Window object can be assumed.<br />

<strong>The</strong> string passed to any dialog like an alert may be either a variable or the result of an<br />

expression. If you pass another form of data, it should be coerced into a string. All of the<br />

following examples are valid uses of the alert() method:<br />

alert("Hi there from <strong>JavaScript</strong>! ");<br />

alert("Hi "+username+" from Javascript");<br />

var messageString = "Hi again!";<br />

alert(messageString);<br />

An alert window is page modal, meaning that it must receive focus and be cleared before the<br />

user is allowed to continue activity with the page.<br />

Note A good use of alert dialogs is for debugging messages. If you are ever in doubt of where a<br />

script is executing or what current variables are set at and you don’t want to use a<br />

debugger, you can use an alert to display useful debugging information.<br />

Confirm<br />

<strong>The</strong> confirm() method for the window object creates a window that displays a message for a<br />

user to respond to by clicking either an OK button to agree with the message or a Cancel<br />

button to disagree with the message. A typical rendering is shown here.<br />

<strong>The</strong> writing of the confirmation question may influence the usability of the dialog significantly.<br />

Many confirmation messages are best answered with a Yes or No button rather than OK or<br />

Cancel, as shown by the dialog at right.<br />

Unfortunately, using the basic <strong>JavaScript</strong> confirmation method, there is no possibility to change<br />

the button strings. However, it is possible to write your own form of confirmation.<br />

<strong>The</strong> basic syntax of the confirm() method is

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!