Alert: Modal Message Box

JavaScript FAQ | JavaScript Dialogs FAQ  

Question: How do I display an alert message from JavaScript?

Answer: To display an alert message (a simple modal message box), use the alert() method:

window.alert('Message goes here')

The button above was created using the following code:

<form name=myform>
<input type=button value="Try it now" 
onClick="alert('Hello from JavaScript!')">
Note that alert() is a method of the window object. Thus, all of the following JavaScript statements produce similar results:
window.alert('message')  // reference to current window
self.alert('message')    // reference to current window
top.alert('message')     // top-level frameset
parent.alert('message')  // parent frame in a  frameset

Returned value. The alert() method does not return a value (that is, it returns undefined).

See also other JavaScript dialogs:

  • Confirm dialog
  • Prompt dialog
  • Print dialog
  • Find dialog
  • Add Favorite dialog
  • Copyright © 1999-2011,