Call a JavaScript function inside body of a web page | About Publisher

Call a JavaScript function inside body of a web page

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.

<body onLoad="javascript:myfunction()" >
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.

<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>
Share this article please, on :
Share on fb Tweet Share on G+

0 Response to "Call a JavaScript function inside body of a web page"

Post a Comment