Window alert()

Example

Display an alert box:

alert("Hello! I am an alert box!!");
Try it Yourself »

More examples below.


Description

The alert() method displays an alert box with a message and an OK button.

The alert() method is used when you want information to come through to the user.

Note

The alert box takes the focus away from the current window, and forces the user to read the message.

Do not overuse this method. It prevents the user from accessing other parts of the page until the alert box is closed.

See Also:

The confirm() Method

The prompt() Method


Syntax

alert(message)

Parameters

Parameter Description
message Optional.
The text to display in the alert box.

Return Value

NONE


More Examples

Alert box with line-breaks:

alert("Hello\nHow are you?");
Try it Yourself »

Alert the hostname of the current URL:

alert(location.hostname);
Try it Yourself »

Browser Support

alert() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.