JavaScript Events – JavaScript Tutorial

This “JavaScript Events” post giving complete information about what is event, how javascript code reacts to events, how events execute at the time of page loading and after page loading and much more.


JavaScript Events:

  • Event is nothing but an action performed on Object.
  • Examples
    1) Loading HTML page.
    2) clicking on a button.
    3) entering text into the text field.
  • A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element.

 

<html>
<head>
<title>EventReaction</title>
<script type=”text/javascript”>
function fun1()
{
alert(“page loaded”);
document.write(“welcome to all”);
}
</script>
</head>
<body onload=”fun1()”>
</body>
</html>

 

<html>
<head>
<title>EventReaction</title>
<script type=”text/javascript”>
function fun1()
{
alert(“page loaded”);
document.write(“welcome to all”);
alert(“New alert”);
document.write(“Appending the text……..”);
}
</script>
</head>
<body onload=”fun1()”>
</body>
</html>

 

  • Head and body logic executes simultaneously.
  • All the instructions either in head location and body location executes sequentially.
    <html>
    <head>
    <title>EventReaction</title>
    <script type=”text/javascript”>
    function fun1()
    {
    alert(“page loaded”);
    <!– document.write(“welcome to all”); //it overrides the page when executed.–>
    }
    function fun2()
    {
    alert(“thank you for click on button”);
    }
    </script>
    </head>
    <body onload=”fun1()”>
    <input type=”button” value=”clickme” onclick=”fun2()”>
    </body>
    </html>

 

Share this