Skip to main content

Call javascript in body tag on different events

JavaScript can be called inside <body> tag of a web page. Mainly JavaScript functions are called on events like Loading a web page, Clicking on a button, Moving mouse, Focusing on an element, etc. For each of these events, there are defined methods and they are fired at that particular event.

For example; onclick() is triggered on a mouse click event while onmouseover() is called when mouse moves over an element. But you can call JavaScript functions even without any event.

Available events for body tag are...

  • ONCLICK : mouse button clicked
  • ONDBLCLICK : mouse button double-clicked
  • ONMOUSEDOWN : mouse button is pressed
  • ONMOUSEOVER : mouse moved onto an element
  • ONMOUSEMOVE : mouse moved over an element
  • ONMOUSEOUT : mouse moved out of an element
  • ONMOUSEUP : mouse button is released
  • ONKEYPRESS : key pressed and released
  • ONKEYDOWN : key pressed
  • ONKEYUP : key released
There are two special events that are specific to <body> tag. Those are;
  • ONLOAD : document loaded completely
  • ONUNLOAD : document unloaded

Calling a JavaScript method

At any of the above events, you can call any JavaScript function from your <body> tag. A JavaScript function named testAlert() can be called as below.
<body onload="testAlert();">

To call move than one JavaScript function, you have to write the function names separated by semi-colons ( ; ) as below.
<body onload="validate(); calculate(); submit();">


  1. This comment has been removed by a blog administrator.


Post a Comment

We appreciate your opinions, suggestions and criticism.

Popular posts from this blog

Web Services with Apache Axis 1.4 Tutorial: server and client sides

Java Sorting: Comparator vs Comparable Tutorial

Creative Commons License Digizol by Kamal Mettananda is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License .
URL of this page must be supplied in attribution
© 2004-2017