Skip to main content

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>

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. This comment has been removed by a blog administrator.

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

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

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

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

    ReplyDelete
  10. yes very helpful and concise info.

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

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

    ReplyDelete
  13. nice work dude..
    thanks..

    ReplyDelete
  14. nice...
    thanks dude

    ReplyDelete
  15. This comment has been removed by a blog administrator.

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. thanks it helped me a lot

    ReplyDelete
  19. thanks it helped me a lot

    ReplyDelete

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