Call a javascript function inside body of a web page - Digizol6

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Thursday, July 5, 2007

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>

18 comments:

  1. Thanks. I was looking for this.

    ReplyDelete
  2. Thanks. It helped me a ton!

    ReplyDelete
  3. Shouldn't it be function doSomething('params'){ in the head section?

    Thanks, though. Was looking for this...as well.

    ReplyDelete
  4. What if the function needed to manipulate an object in the document.? We know that the object is in the body which hasn't been loaded yet.

    ReplyDelete
  5. tnnks a ton......i am extremenly happy after getting this code...thnx a ton ton ton..once again

    ReplyDelete
  6. thnx a ton....i was glad after getting this code..thnx once again

    ReplyDelete
  7. why it does not work for me? what i am doing wrong?

    ReplyDelete
  8. thanx a ton for the suggestion ... was of great help

    ReplyDelete
  9. yes very helpful and concise info.

    ReplyDelete
  10. This helped me a ton. Could not understand why my textbook instructions were not working.
    Thanks Thanks thanks.

    ReplyDelete
  11. Thanks! Concise answer to a silly/annoying question

    ReplyDelete
  12. nice work dude..
    thanks..

    ReplyDelete
  13. nice...
    thanks dude

    ReplyDelete
  14. thanks it helped me a lot

    ReplyDelete
  15. thanks it helped me a lot

    ReplyDelete

Post Top Ad

Responsive Ads Here