I'm familiar with the typical use of onload
, as in the following:
<body onload="alert('Hello, World!');">
...
</body>
What are all the html elements that fire a load event? (thus executing javascript supplied in an onload attribute)
For example, img
is one such tag that will execute the javascript supplied in an onload
attribute when some.png
has loaded:
<img onload="someImgLoaded()" src="some.png" />
'onload' is supported by the following HTML tags:
And the following Javascript objects:
Below is a much more comprehensive list of elements that fire a load event when the requested resource finishes downloading:
For the most coverage, it's best to consider that all html elements referencing a url will result in a request and fire a
load
orerror
event when that request for the succeeds or fails. So, basically, any element with asrc
orhref
attribute, except for these tags:And including the
body
tag, because it ironically doesn't have asrc
ORhref
attribute.Below is some rough javascript for discovering these elements:
Also, with the "everything with src or href" method, you ignore irrelevant or other tags that typically have a src or href attribute, but not always.
Other things that can have network failures:
onload
andonerror
attributes can be useful to keeping track of whether or not your user has an active internet connection, which is something I'm attempting to address with my library check-online.js: http://github.com/devinrhode2/check-onlineThere is some obvious testing to be done to see whether or not
onload
is an event specific to thebody
,frame
,iframe
,img
,link
, andscript
elements. Basically anything which represents a resource to be loaded. Forbody
, that is the document in question. For the others, each is fairly obvious.According to this page, you can use
onload
with:<body>
,<frame>
,<frameset>
,<iframe>
,<img>
,<link>
, and<script>
.Many elements have the onload event. You can find them here
But if you want to test the loading of the DOM, then it's best to use the window.onload. It's also recommended that you separate the javascript code from the HTML markup.