Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: <body onload=""> and window.onload

I ran across a weird error today. I had a simple userscript which was adding a window.onload event to all the sites. But one of the sites had a <body onload="func();"> defined. What happened is that window.onload was working as usual but <body onload=""> stopped working for the site after installing userscript.

When I used window.body.onload instead both worked well. I know that window.onload and <body onload=""> are different way of doing the same thing but what is happening in window.body.onload which makes it work well with <body onload="">?

like image 523
vikmalhotra Avatar asked Dec 14 '25 16:12

vikmalhotra


1 Answers

Window.onload and body tag's onload are the same event. So if you set the same event twice by two different methods it will only end up with the one value - one of the functions.

window.body.onload is a seperate event. Just another quirk of the DoM and what broswers do with it.

like image 140
Myforwik Avatar answered Dec 17 '25 06:12

Myforwik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!