0👍
✅
You have a loop there, and in each iteration of the loop, you’re using window.onload = function() {…};
Basically, you’re overwriting the onload
function at each iteration of the loop, so only the last overwrite survives.
What you want is to add load
event listeners on the window instead of continually overwriting one single listener.
To achieve this, I would recommend replacing this:
window.onload = function () {…}
with this:
window.addEventListener('load', function() {…})`
See addEventListener
Another way of seeing that window.onload = function () {…}
is problematic is the following snippet:
window.onload = function () { alert('one') };
window.onload = function () { alert('two') };
Q: What do you expect to happen on page load here?
A: Similarly to your case, only the second function (alerting "two") will be called.
Source:stackexchange.com