Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onload event on <form> tag

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')>
like image 615
Sudarshan Taparia Avatar asked Apr 20 '15 14:04

Sudarshan Taparia


People also ask

Does onload work on Div?

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.

Which HTML elements support onload?

According to this page, you can use onload with: <body> , <frame> , <frameset> , <iframe> , <img> , <link> , and <script> .

Is onload an event handler attribute?

You can assign an onload event handler directly using the onload attribute of the <img> element, like this: <img id="logo" src="logo.

How do you trigger onload?

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.


2 Answers

try adding the onload to the body tag

<body onload="alert('Hello World')">
like image 57
TheSk8rJesus Avatar answered Sep 29 '22 14:09

TheSk8rJesus


//  javaScript
window.onload = function{
   alert("loaded");
}


//  jquery
 $(document).ready(function() {
    $("#div-id").click(function(){ 
    alert("the page is loaded");
    }); 
});
like image 21
Careen Avatar answered Sep 29 '22 14:09

Careen