Program to display an alert box |HTML|

Introduction

Alert boxes are a simple yet effective way to communicate with users on a web page. Whether you want to display a message, prompt for confirmation, or provide feedback, JavaScript's built-in alert() function comes in handy. In this blog post, we'll explore how to create and use alert boxes in JavaScript.

    JavaScript alert boxes are pop-up dialog boxes that display messages to users. They are commonly used to provide information, warnings, or confirmations. The alert() function is used to trigger an alert box, which pauses script execution until the user dismisses the alert by clicking "OK".

Code

    
     
          <html>
          <head>
              <title>JavaScript Alert Box Example  </title>
              <script type="text/javascript">
                // JavaScript function to display an alert box
                function showAlert() {
                    alert("I am an alert box");
                }
              </script>
          </head>
          <body>
              <input type="button" onClick="showAlert()" value="Click Me"/>
          </body>
          </html>
        
       
     


Output







Explanation

  • We've defined a JavaScript function called showAlert() that triggers an alert box using the alert() function.
  • The HTML button <input> element has an onClick attribute set to showAlert(), meaning that when the button is clicked, the showAlert() function is executed.
  • The value of the button is "Click Me", which serves as the label for the button.

Conclusion

     JavaScript alert boxes are a simple yet effective way to communicate with users on a web page. They can be used to display messages, warnings, or confirmations, providing users with important information or prompting them for input. By incorporating alert boxes into your web pages, you can enhance user interaction and improve the overall user experience. Experiment with different messages and scenarios to see how alert boxes can enhance your web projects!

Post a Comment

0 Comments