Window alert() Method

Definition and usage

alert() method to display an alert box with a message and an OK button.

If you want to pass information to the user, you can use alert() method.

Prompt

The alert box will remove the focus from the current window and force the user to read the message.

Please do not overuse this method. It will prevent the user from accessing other parts of the page until the alert box is closed.

See also:

confirm() method

prompt() method

Example

Example 1

Display the alert box:

alert("Hello! I am an alert box!!");

try it yourself

Example 2

Alert box with line breaks:

alert("Hello\nHow are you?");

try it yourself

Example 3

Prompt the current URL's hostname:

alert(location.hostname);

try it yourself

syntax

alert(message)

parameter

parameter description
message optional. The text to be displayed in the alert box.

return value

none.

technical details

description

alert() will be displayed in the dialog. message is displayed to the user. This dialog contains an OK button, which the user can click to close the dialog. The dialog is usually modal, and the method will remain in the dialog until the user closes it.alert() invocation will pause execution.

convention

an error message will be displayed when invalid information is entered into certain form elements, which may be alert() is the most common usage of the method. The warning dialog can inform the user of the problem and suggest what the user should correct to avoid this error.

alert() The appearance of the method dialog is determined by the platform, but it usually has graphics to represent errors, warnings, and some types of warnings. alert() Any message can be displayed, but the warning graphic in the dialog box indicates that this method is not suitable for displaying simple informational messages, such as 'Welcome to my website' and so on.

Note

displayed in the dialog box message It is a plain text string, not an HTML-formatted string. Line breaks can be used within this string. \nMessages can be displayed in multiple lines. Spaces can also be used for some basic formatting, and underlined characters can simulate horizontal rulers, but the final effect largely depends on the font used by the dialog box, so it is system-dependent.

Browser Support

All Browsers Support alert():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support