In the following, I am assigning the click event to func(). When I load the page, the alert box shows up. Can someone explain why the event is being triggered? What exactly is happening behind the scenes? Do all events attached to a function get triggered when the DOM loads?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
window.onload = function initialize() {
func.click += handler();
}
function handler(e) {
alert('clicked');
}
function func() {
}
</script>
</head>
<body>
</body>
</html>