What is the difference between the following two JavaScript snippet code. They behave differently.
window.onload = function()
{
mainForm.onsubmit = function(e) {
alert("Submit");
}
}
and
window.onload = my_func();
function my_func() {
mainForm.onsubmit = function (e) {
alert("Submit");
}
}
when the following HTML code is used. Once submit button is pressed I get a message 'Submit' from the first snippet but nothing from the second one.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>JS Test</title>
<script src="test.js"></script>
</head>
<body>
<form method="get" action="" id="mainForm">
<input type="submit">
</form>
</body>
</html>
Thank you in advance.