Skip to content
Advertisement

Check if ‘onload’ would have fired already?

I have a situation where I’d like to set an onload handler for a script element, but it is possible that the script element has already loaded before I do so. If it’s already loaded, I’d like to detect that and run my handler immediately.

What would be a reliable way of checking a script element’s onload state after the fact?

BTW I know that IE uses readyState instead of an onload event so I don’t need help for that.

Advertisement

Answer

I don’t believe that you reliably know this– if you miss an event, then you’ve missed it. The simplest solution if you really can’t know whether the script has already been loaded is to have the bottom of the script set some global flag indicating that it’s been loaded; then you can check that, and set your onload handler if it’s not present.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement