How to Create an Alert Box in Javascript


In this article, we show how to create an alert box in Javascript.

To create an alert box in Javascript, you use the alert() command.

Inside of the alert() parentheses, you add a single quotes (' ') and inside place whatever text that you want displayed inside of the alert pop-up. For example, if you want to display the word "Warning", you would place this inside of the single quotes.

When the page is downloaded containing an alert command, the alert box will pop-up right from the beginning, just as it did with this page.

Below is an example of a web page containing a javascript alert pop-up that displays the text 'Warning!':

This is all that is needed to create an alert box using Javascript.

The alert box can be placed anywhere in the HTML page. By convention, though, it is normally placed somewhere near the very beginning, because typically we want this displayed as soon as the page is loaded. So having it right at the top is normally convenient. This is a reason why we wouldn't want to put it dead bottom, last, at the end of a web page. However, it doesn't matter. As long as it's somewhere on the page, it will load and the alert box will come up.

And it doesn't matter whether the web page is an HTML file (.html extension), a PHP file (.php extension), or any other type of web file. As long as the web browser supports Javascript (as most do), it will be able to render and execute the javascript code on the page.

HTML Comment Box is loading comments...