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
How to Handle <Tab> in Textarea
Remove Whitespaces Inside a String in JavaScript
JSON Object Undefined in Internet Explorer 8
HTML Table with Fixed Headers and a Fixed Column
Putting HTML Inside an Iframe (Using JavaScript)
Change <Audio> Src with JavaScript
Passing a Variable from Node.Js to HTML
Emailing Google Sheet Range (With or Without Formatting) as a HTML Table in a Gmail Message
Multicolor Text Highlighting in a Textarea or Text Input
How to Add Custom HTML Attributes in Jsx
Property 'Submit' of Object #<Htmlformelement> Is Not a Function
Jquery Slider, How to Make "Step" Size Change
How to Automatically Set the Focus to a Textbox When a Web Page Loads
How to Create an Asynchronous Function in JavaScript
Why Does Typeof Array with Objects Return "Object" and Not "Array"