JavaScript functions can be automatically invoked without an event.
JavaScript is mainly used for actions on user events like onClick(), onMouseOver() etc. But what if you need to call a JavaScript function without any user events? Answer would be to use the onLoad() of the <body> tag.
But what if you don't have access to change the onLoad()? Is there an answer for that as well?
Yes, and it's easy. Just call the function inside your page as same as you would write JavaScript inside the body of a page.
In this example, doSomething() function is added to the web page inside the header of the page. And for ease of understanding the complete code is shown below.
<body onLoad="javascript:myfunction()" >
Yes, and it's easy. Just call the function inside your page as same as you would write JavaScript inside the body of a page.
<script type="text/javascript" language="JavaScript">
doSomething('params');
</script>
In this example, doSomething() function is added to the web page inside the header of the page. And for ease of understanding the complete code is shown below.
<html>
<head>
<script type="text/javascript" language="JavaScript">
function doSomething(params);
//do something nice with params
}
</script>
</head>
<body>
This page does call a JavaScript function when the page is loaded,
without using the onload() event call.
<script type="text/javascript" language="JavaScript">
doSomething('blue');
</script>
</body>
</html>
COMMENTS