Back to ICS415 Main

window.alert()

 

Contents

 

1.0 Overview
1.1 Syntax
1.2 HTML Example
1.3 Output Example
1.4 Sample
1.5 Description
1.6 Reference

 

Overview

 

A alert() method is one of the methods applied to windows object. The alert() mothod is categorized as the method to "display simple dialog box" as well as confirm() and prompt(). It pops up the dialog box to display to the users, and gives them an OK button to confirm the message popped up. Note that the text can display only plain text, not HTML-formatted text so that space, new lines, and various punctuation characters can be used.

The alert() method does not return until the user dismisses the dialog box with the OK button pushed. In other words, the code stops running and loading until the user responds with the required input, say pushing the OK button.

Syntax

 

window.alert(text)

Text is the string of text you want to display in the alert window.

 

HTML Example

 

<HTML>
<HEAD>
<TITLE>alert() Method Program</TITLE>

// Lets browsers know Javascript used as script language
<SCRIPT LANGUAGE="JavaScript">
// prevents old browsers from printing the script code
<!--
  
// clickMe() function to prints alert message on the window
  function clickMe() {
    
//alert() prints the text inside ""
    window.alert("You clicked me, didn't you");
  }
//-->
</SCRIPT>
</HEAD>

<BODY>
<H1>Simple Alert() Method</H1>

<FORM>
  
//calls clickMe() method when the button is clicked (onclick)
  <INPUT TYPE="button"
         VALUE="Click me!"
         onclick="clickMe()">
</FORM>

</BODY>
</HTML>


 

Output Example

 

When the HTML code above runs and you click the "Click me!" button, then the alert window pops up.

Sample

 

Click the button below to execute Javascript.

 

Description

 

In the <Script> tag, function clickMe() defines the procedure of its method. If the function is called, the method (alert()) of the window object (window) is triggered. In other words, window.alert() triggers the alert method. Since alert() method prints out the text inside the parenthesis on the new window, the new window pops up with the text massage, "You clicked me, didn't you?" ( " is not displayed).

Note that <!--, //--> tag is used for the purpose of the old browsers, which could not recognize the Javascript. If you do not have the comment tag, the older browsers print the Javascript code in the browser window. That's how to prevent the Javascript code from being printed on the browser.

In the <Form> tag, button is set with the button name "Click me!" and onclick event handler. If the button is clicked, the event hander, onclick, is triggered so that clickMe() function defined in the <Head> tag is called. Thus, clickMe() function proceeds to show "You clicked me, didn't you?" triggered by alert() method.

Reference

 

Window Object http://www.w3schools.com/js/js_window.asp
Alert Method http://www.pageresource.com/jscript/jalert.htm

 

 

 

© 2002 TAKUYA YAMASHITA ALL RIGHTS RESERVED
Back to top