JavaScript Call Funtion After Page Load

Usually we call a function during page on load with following two methods.


<body onload="happycode() ;">

or


<script>
window.onload=happycode ;
</script>

But how can we call a JavaScript function after page is loaded?. I solved it by using a very simple method, it did exactly what i want and call after page and content is 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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
37 Comment threads
9 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
43 Comment authors
JunnAlvinV.L.Lee MinJosemambobananapatch Recent comment authors
newest oldest most voted
elbandito
Guest
elbandito

Very simple – thanks

Heiner
Guest
Heiner

wwwwwiiiiii! thanks! exactly what i need!

Kumar
Guest
Kumar

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?

ForumPoster
Guest
ForumPoster

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?

Amal Roy
Guest
Amal Roy

Thanks a lot friend.

Neeraj
Guest
Neeraj

No need to set window.onload function at the end of page…

Asa
Guest
Asa

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!

Matt
Guest
Matt

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

sokolah974
Guest
sokolah974

Great that did the trick. As mkyong wrote, we sometimes think too much !!!
Thanks.

Tausif,Vinita
Guest
Tausif,Vinita

Thanks, you did a great job.

Rams
Guest
Rams

Thanks a lot..! A very handy and useful tip.

Viet
Guest
Viet

same look it not work with external javescript function

ashish
Guest
ashish

So simple,Thanks

Harish
Guest
Harish

Simple and Brilliant. As they say “Hit the nail on its head”.

Good that I found this tip before I pluck my hair 🙂

Thanks

Harish

ibrar
Guest
ibrar

Excellent idea 🙂
thanks

Luis Santos
Guest
Luis Santos

Thanks a lot this helped me…

K L Abhilash
Guest
K L Abhilash

Hi this worked fineeeeee.

Gardhi
Guest
Gardhi

Great code. I spent couple of hours to find it. That is what I need.

shimray
Guest
shimray

works pretty fine. thanks!

sgor
Guest
sgor

Thatz great man.. now am loadimg my cpm advertise link with page load

Hunter
Guest
Hunter

Works well if you only need the page to load; however, it will still execute before other elements are loaded (ie images).

Aara
Guest
Aara

Thanks a lot !!

Aylian
Guest
Aylian

thnx bro u save my valubl time, keep this good work.tc bs.

Tarik
Guest
Tarik

That is works. Thx. Additionally if u call more than one function you can use :

window.onload = function () {
func1();
func2();
etc.
};

on that style.

Aun Rizvi
Guest
Aun Rizvi

are you sure it is a standard way to do this? some browsers put the script inside the body automatically and when you get body.innerHTML you will also get the script tag !! refer this http://stackoverflow.com/questions/3037725/is-it-wrong-to-place-the-script-tag-after-the-body-tag

OMGWEEGEE2
Guest
OMGWEEGEE2

Danka, now I can be able to add the print function to one of my webpages so it automatically shows the printing prompt and let’s the user print out a christmas card I can create!

Jonathan
Guest
Jonathan

I have a javascript which I would like to activate automatically when loading my facebook group. The code works fine if I go to my group on facebook and add it as a bookmark containing the javascript as its url. I want to know how I can place on my website http://www.gibwork.com/gibworkadd with a hyperlink “click here” which takes them to my facebook group page and later activate the javascript? Some members are not browser savvy and take ages trying the method of creating a bookmark and making sure they click it whilst on the groups page or simply adding… Read more »

sonu sindhu
Guest
sonu sindhu

thanks so much …

Farid Wong
Guest
Farid Wong

Loads of Thanks to mkyong!