I want a JavaScript function to execute when the page loads.
I added an onload
attribute to my <form>
tag, but the alert
is not executed.
What am I doing wrong?
<form name=myfm method=post action=quiz.php onload = alert('hello')>
The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.
According to this page, you can use onload with: <body> , <frame> , <frameset> , <iframe> , <img> , <link> , and <script> .
You can assign an onload event handler directly using the onload attribute of the <img> element, like this: <img id="logo" src="logo.
Yeah, you can use a click event called onLoad() . Just use the setTimeout() method in jquery. It will call a click function without clicking it.
try adding the onload
to the body
tag
<body onload="alert('Hello World')">
// javaScript
window.onload = function{
alert("loaded");
}
// jquery
$(document).ready(function() {
$("#div-id").click(function(){
alert("the page is loaded");
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With