For example, when an alert window pops up, the user has to close it manually in order to proceed with their work.
Alerts can be used in multiple ways:
Hence, alerts are a useful tool to learn!
You can create a button that will display an alert message when clicked. The example below shows you how to do this.
An alert message can simply be shown when the HTML file runs, before the user accesses any other elements on the page. The example below shows how to do this.
In the above snippet, the
alert method is called in the
head section of the HTML file as this allows the message to be shown before the rest of the page is visible. Note that without a button, the
alert code is written within the
Alerts dont just show text values. They can also be used to show variable values created within the file. The code example below shows how this is done.
Note that in the snippet below, when passing a variable to the
alert method, you are not required to put the variable name in “double quotes”. This signifies that the value given to the
alert method is a variable.
As you can see in the below code, from line 4 to 8, we can even put
alert messages in methods and call them when needed. This way the same message can be shown in multiple places without having to re-write the code.
View all Courses