What Is Events

events are actions that happens as a results of the user action or as results of phase change of the weather of a DOM tree.

Client-side scripting languages like JavaScript, JScript, ECMAScript, VBScript, and Java can register various event handlers or listeners on the element nodes inside a DOM tree, like in HTML, XHTML, XUL, and SVG documents.

Page, DOM and Browser loading In JavaScript

This is an example to explain the variations of load events.

1).onload event

<body onload="someFunction()">
<img src="image1" />
<img src="image2" />
</body>
<script>
function someFunction() {
console.log("Hi! I am loaded");
}
</script>

In this case, the message is logged once all the contents of the page including the images and stylesheets(if any) are completely loaded.

DOMContentLoaded event In JavaScript

document.addEventListener("DOMContentLoaded", function(event) {
console.log("Hello! I am loaded");
});

In the above code, the message is logged only after the DOM/document is loaded (ie:once the DOM is constructed).

Self-invoking anonymous function

(function(){
console.log("Hi I am an anonymous function! I am loaded");
})();

Here, the message gets logged as soon as the browser interprets the anonymous function. It means, this function can get executed even before the DOM is loaded.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Events In JavaScript

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password