Ad

What Is Meant By "onload=foo(event)"?

- 1 answer

I am new to JS and found this code section:

<body>
    <span id="sp1">
        <img src="aurora.gif" onload="ev1(event)">
    </span>        
</body>

As far I understood, the onload attribute gets triggered if the entire HTML Doc finished "loading".
But I don't really understand, whats passed to the ev1 function if the onload attribute is triggered. What is event in this case?

Here is the source code

Ad

Answer

It's hooking up a handler to the image's load event and passing the event object for the event into the handler. The event object exists reliably cross-browser within the context of the onload attribute-style event handler: On IE, it's a global; on Firefox, it's a local within a synthetic function created for the onload handler; on Chrome, I think it's both. :-)

Note that the ev1 function must be a global, which is one of many reasons not to use onxyz-attribute-style event handlers.

Ad
source: stackoverflow.com
Ad