I’ve read about the technical differences such as which option waits for the appropriate elements to load, but for most cases I understand that they both serve a similar purpose.
I’d like to know about the rationale behind needing to call the function (say, initialize();) when using but only using the function name if writing window.onload=myFunction. (MDN calls it a handler function). Here, I’m referring to the difference being the missing parentheses.
Is there an advantage to either approach? A technical reason?
It’s the difference between HTML markup and the Document Object Model that Javascript uses. When you run:
That is setting the property “onload” to that function object, which is to handle the event. If on the other hand, you wrote:
that would execute fn immediately, and set the onload property to whatever value the function returns (which could also work, if the function fn returned another function).