What HTML Tags Support the Onload/Onerror JavaScript Event Attributes

What html tags support the onload/onerror javascript event attributes?

'onload' is supported by the following HTML tags:

<body>, <frame>, <frameset>, <iframe>, <img>, <link>, <script>

And the following Javascript objects:

image, layer, window

What html tags support the onload javascript event?

The specs for html4 only mention body and frameset: http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.3

I think it's browser dependent.

How do I put tag attributes onload & onerror in the javascript?

This way.

Create attribute and add it to script element.

The createAttribute() method creates an attribute with the specified name, and returns the attribute as an Attr object.

attribute.value property is used to set the value of the attribute.

element.setAttributeNode() method is used to add the newly created attribute to an element.

function checkScriptLoaded2(){  alert("Error in loading.")}
var sNew = document.createElement("script");sNew.async = true;sNew.src = "https://example.com/script.js?"+Math.random();

sNew.setAttribute('onerror', 'checkScriptLoaded2();') // Shorthand as suggested by Felix
var att = document.createAttribute("onload"); // Elaborated way att.value = "scriptLoaded2=true;"sNew.setAttributeNode(att)

var s0 = document.getElementsByTagName('script')[0];s0.parentNode.insertBefore(sNew, s0);
<div>hello</div>

Which HTML elements support onerror attribute?

I believe the list of supported events on <script> and <link> at pieisgood is what you're looking for.

As you can see, onload is the most widely supported for <link> which is very lacking in working events. But you could try to implement an error event by having onload clearTimeout for some function that assumes an error happened.

HTML onload Event in link element doesn't work

Based on https://www.w3schools.com/tags/ev_onload.asp onload event attribute can be used with tags:

<body>, <frame>, <frameset>, <iframe>, <img>, <input type="image">, <link>, <script> and <style>

You are NOT using <link> tag. You are using anchor(<a>) tag on which this attribute is not supported!
<link> tag is used to link an external resource to the document. For example CSS file.

<head>
<link rel="stylesheet" type="text/css" href="theme.css">
</head>

How to add onload event to a div element

No, you can't. The easiest way to make it work would be to put the function call directly after the element

Example:

...
<div id="somid">Some content</div>
<script type="text/javascript">
oQuickReply.swap('somid');
</script>
...

or - even better - just in front of </body>:

...
<script type="text/javascript">
oQuickReply.swap('somid');
</script>
</body>

...so it doesn't block the following content from loading.

How does one use the onerror attribute of an img element

This works:

<img src="invalid_link"
onerror="this.onerror=null;this.src='https://placeimg.com/200/300/animals';"
>

Live demo: http://jsfiddle.net/oLqfxjoz/

As Nikola pointed out in the comment below, in case the backup URL is invalid as well, some browsers will trigger the "error" event again which will result in an infinite loop. We can guard against this by simply nullifying the "error" handler via this.onerror=null;.



Related Topics



Leave a reply



Submit