Can I use body onload and window.onload at the same time? I've tried it using this code
<body onload = "alertFirst()">
</body>
<script>
window.onload = alertSec;
</script>
But it didn't work. I just need someone to confirm it to me. Many thanks
If one or more of the scripts you want to use has the event handler in the BODY HTML tag, you can still move it to into javascript code. See the example below:
Script #1:
Script #2:
Result:
I think it may can help you.
The answer to your question is "no". However there are ways around it.
Adding both calls to one onload function is ideal, but if you /have/ to add an onload handler after one is already added, and you are not using a framework which facilitates this, you can get by like this:
No,
document.body.onload
is actually mapped towindow.onload
. You can check yourself—when you have<body onload="a()">
and toconsole.log(window.onload)
,a()
is printed out into the console.What you can do is to have one onload event handler that calls two other functions.
or two event listeners
You can (by adding event handler(s)) but you should NOT have both
Instead add the call to the window.onload: