JavaScript Call Funtion After Page Load
Written on July 22, 2008 at 11:03 am by
mkyong
Usually we can call a function during page on load with following two methods.
<body onload="happycode() ;">
or
<script> window.onload=happycode ; </script>
But how we can call a javascript function after a page load? I solved it by using a very simple method, it did exactly what i want and call after page and content loaded. Just add an onload function at the end of the body.
<html>
<script language='javascript'>
function happycode(){
alert('helo');
}
</script>
<body>
<h1>Javascript call after page loaded</h1>
<script>
//call after page loaded
window.onload=happycode ;
</script>
</body>
</html
This article was posted in Javascript category.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
There are many better ways of doing this. You can load your site even more faster by loading JavaScript files and references after content
http://www.tutkiun.com/2010/07/load-javascript-after-pageload.html
Brilliant! Thanks Mkyong!
I work for a company that only allows me to edit blocks and won’t give me access to the website layout. I just installed a rotating image function in my block and your code activated it!
No need to set window.onload function at the end of page…
Thanks a lot friend.
Great, this would be handy if we could call out external javacript like this.
For instance instead of putting this in our head (or at the bottom of the page):
call this external sript from an onLoad.. but how?
How can I call that external scipt only to be called/loaded after the page has loaeded?
article updated with full code provided. The “happycode” is a normal javascript function, the point is call this function at the end of the body.
Please show me some sample code (more than shown above):
….
…
//call after page loaded
window.onload=happycode ;
Where and how to define happycode? also where do you call
window.onload=happycode?
wwwwwiiiiii! thanks! exactly what i need!
Very simple – thanks
ya sometime it’s just we think too much