I have a question about the difference between
window.addEventListener('load', function(){ });
and
jQuery(window).bind("load", function () { });
I wanted my script code to execute last (and I can't place it last in the document by the way for other reasons), so I decided to run it until everything is loaded.
Using window.addEventListener('load' worked most of the times but sometimes It did got executed but too early.
jQuery(window).bind("load" is working much better, and seems like it get executed at the right time.
Can someone please tell me is there any diferrence between this two line of codes ?
Edit :
Document ready is executed in the order of document.ready(s), and I have to wait for some document.ready of other scripts in the footer of the page before doing anything. I have to, because I can't place my script after them. So no I can't use document.ready.
I am also working in Magento 1.9 which uses jquery 2.x so bind is not deprecated.
My script modify some elements in the page which are modifed with scripts placed in the footer.
Let's say I select element by class name 'A'. The script in the footer will change it to 'B' and my script will fail. This is why I wait for the last script to modify the element then I execute mine and select element by class name 'B'.
the addEventListener load sometimes execute before the last script which makes it look for element with class name 'B' while there isn't an element with class 'B' yet.
The jquery bind load never did that.