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, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Singh
Guest
Singh

Woah.. thanks bro. You solved my problem. Nice Work. Can you please send JavaScript to me in email to disable copy on my blog.? Waiting for your reply

V.L.
Guest
V.L.

There is typo in title. Should be “Function” instead of “Funtion”.

Jose
Guest
Jose

Simple and great !!!. All your posts are very usefully. Thanks.

Harmeen Kaur Pannu
Guest
Harmeen Kaur Pannu

Hi, there can you make javascript to disable Ctrl+U button my blog. I don’t want visitors to see focus keywords on my blog. People are copying keywords… please reply asap

mambobananapatch
Guest
mambobananapatch

Even if Javascript could conceal your site’s source code, people could disable Javascript and see your site’s source code.

The only way to hide your site’s source code is to surround it in tags.

Suresh
Guest
Suresh

This code is not working in asp.netMvc4 .
before loading its working not working after loading..

Farid Wong
Guest
Farid Wong

Loads of Thanks to mkyong!

sonu sindhu
Guest
sonu sindhu

thanks so much …

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 »
Jonathan
Guest
Jonathan
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!

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

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.

Aylian
Guest
Aylian

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

Aara
Guest
Aara

Thanks a lot !!

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).

sgor
Guest
sgor

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

shimray
Guest
shimray

works pretty fine. thanks!

Gardhi
Guest
Gardhi

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

K L Abhilash
Guest
K L Abhilash

Hi this worked fineeeeee.

Luis Santos
Guest
Luis Santos

Thanks a lot this helped me…

ibrar
Guest
ibrar

Excellent idea :)
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

ashish
Guest
ashish

So simple,Thanks

Viet
Guest
Viet

same look it not work with external javescript function

Rams
Guest
Rams

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

Tausif,Vinita
Guest
Tausif,Vinita

Thanks, you did a great job.

sokolah974
Guest
sokolah974

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

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

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!

Neeraj
Guest
Neeraj

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

Amal Roy
Guest
Amal Roy

Thanks a lot friend.