Add onload event handler by using attribute in both tag and code in JavaScript

Description

The following code shows how to add onload event handler by using attribute in both tag and code.

Example


<html>
<head>
<script type="text/javascript">
function helloMsg() {<!--from   w w w.  j a v  a 2  s. c  o  m-->
var helloString = "hello there";
document.write(helloString);
}

function helloTwice() {
var helloString = "hi again";
document.write(helloString);
}

window.onload=helloTwice;
</script>
</head>
<body onload="helloMsg();">
</body>
</html>

Click to view the demo

The code above generates the following result.

Add onload event handler by using attribute in both tag and code in JavaScript
Home »
  Javascript Tutorial »
    Event »
      Browser Event
Javascript Tutorial Browser Event
Access window event in JavaScript
Add event handler for body onload event in ...
Add event handler for window on load event ...
Add onload event handler by using attribute...
Cancel event with preventDefault in JavaScr...
Filter click event to capture in JavaScript
Filter event to catch in JavaScript
Handle Error event on body in JavaScript
Handle browser window resize event in JavaS...
Handle document on changed event in JavaScr...
Handler document on abort event in JavaScri...