How to use window.alert in JavaScript?

Get support on JavaScript,jQuery, AJAX and other related technology.
Post Reply
Site Admin
Posts: 44

How to use window.alert in JavaScript?

Post by admin » Sat Oct 19, 2019 5:13 pm

Using window.alert()
The alert method displays a visual alert box on screen. The alert method parameter is displayed to the user in
plain text:

Code: Select all

Because window is the global object, you can call also use the following shorthand:

Code: Select all

So what does window.alert() do? Well, let's take the following example:

Code: Select all

alert('hello, world');
In Chrome, that would produce a pop-up like this:

The alert method is technically a property of window object, but since all window properties are
automatically global variables, we can use alert as a global variable instead of as a property of window -
meaning you can directly use alert() instead of window.alert() .

Unlike using console.log , alert acts as a modal prompt meaning that the code calling alert will pause until the
prompt is answered. Traditionally this means that no other JavaScript code will execute until the alert is dismissed:

Code: Select all

console.log('Alert was dismissed');
However the specification actually allows other event-triggered code to continue to execute even though a modal dialog is still being shown. In such implementations, it is possible for other code to run while the modal dialog is being shown.

More information about usage of the alert method can be found in the modals prompts topic.
The use of alerts is usually discouraged in favour of other methods that do not block users from interacting with the page - in order to create a better user experience. Nevertheless, it can be useful for debugging.
Starting with Chrome 46.0, window.alert() is blocked inside an <iframe> unless its sandbox attribute has the value allow-modal.

Posts: 0

Post by anono7 » Sat Oct 19, 2019 6:09 pm

Very easily explained, thanks.

Post Reply