Java News Tips Software
Java News Tips Software | Contact | Facebook | Twitter RSS

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>

Labels: ,


23 Comments

  1. Anonymous Anonymous on July 25, 2007 4:55 PM  
    Thanks. I was looking for this.
  2. Anonymous Anonymous on June 21, 2009 6:11 AM  
    Thanks. It helped me a ton!
  3. Shouldn't it be function doSomething('params'){ in the head section?

    Thanks, though. Was looking for this...as well.
  4. Anonymous Anonymous on July 21, 2009 12:54 PM  
    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.
  5. This comment has been removed by a blog administrator.
  6. Anonymous Anonymous on July 30, 2010 5:39 PM  
    tnnks a ton......i am extremenly happy after getting this code...thnx a ton ton ton..once again
  7. thnx a ton....i was glad after getting this code..thnx once again
  8. no it doesn't
  9. why it does not work for me? what i am doing wrong?
  10. thanx a lot for the info :)
  11. thanx a ton for the suggestion ... was of great help
  12. Anonymous Anonymous on July 31, 2011 9:31 AM  
    yes very helpful and concise info.
  13. This helped me a ton. Could not understand why my textbook instructions were not working.
    Thanks Thanks thanks.
  14. Anonymous Anonymous on November 14, 2011 9:34 PM  
    Thanks! Concise answer to a silly/annoying question
  15. Anonymous Anonymous on July 18, 2012 10:15 AM  
    nice work dude..
    thanks..
  16. Anonymous Anonymous on July 26, 2012 1:29 AM  
    nice...
    thanks dude
  17. Anonymous Anonymous on January 17, 2014 2:55 PM  
    This comment has been removed by a blog administrator.
  18. Anonymous Anonymous on January 30, 2014 12:01 AM  
    This comment has been removed by a blog administrator.
  19. Anonymous Anonymous on March 20, 2014 9:17 AM  
    This comment has been removed by a blog administrator.
  20. Thanks for the tip!
  21. Anonymous Anonymous on May 02, 2016 2:34 PM  
    thanks it helped me a lot
  22. Anonymous Anonymous on May 02, 2016 2:35 PM  
    thanks
  23. Anonymous Anonymous on May 02, 2016 2:36 PM  
    thanks it helped me a lot
ABOUT AUTHOR
Page Views :
Email :
PREVIOUS ARTICLES
Select Month:
TOP
Free counter and web stats