Can we use onload in div?

Sometimes you need to call javascript function after div load, or we need to execute a function after the div load. In this post, I will provide you the technique to solve how to call a javascript function after div load.

Can we use onload in div?

The Main Idea For Call A Js Function After Div Load

Suppose you have a div called my_area(id), now you want to call a javascript function after the my_area div load completely.
Now think logically if your div(my_area) is loaded then it must have a width and if it is not loaded then it has no width.
So check the width of your div and if you found that it has div then call your function.
Also, you need to check the width until your fund width is greater than zero because you do not know when it will load and if you found that means your div's width is greater than zero then stop checking and call your function.

HTML code of "call javascript function after div load"

<div id="my_area">
	call a javascript function after load this div
	ID of this div is my_area
</div> 

"Call Javascript Function After Div Load" Raw JavaScript

var checkDiv = setInterval(function(){

var my_div_width = $("#my_area").width(); // find width

if( my_div_width > 0) { 
	clearInterval(checkDiv);
	 	aTestFunction();
	}
}, 10); // check after 10ms every time

function aTestFunction(){
	return "called";
}
Note: Using setInterval you can chack your div's width continuously after every 10ms, using clearInterval you can stop checking.

"Call Javascript Function After Div Load" Jquery Example

document.querySelector('#my_area').addEventListener('load', function(){
  aTestFunction();
});

function aTestFunction(){
	return "called";
}
The a "aTestFunction" will call after the my_area div loaded.

The HTML DOM onload event in HTML occurs when an object has been loaded. The onload event is mostly used within the <body> tag, in order to run the script on the web page that will load all the content completely. The onload event can be used to check the user’s browser type and browser version, and load the version of the web page based on the information. The onload event can also be used for cookies. 

Syntax:

In HTML:

<element onload="scriptFile">

In JavaScript:

object.onload = function(){scriptFile};

In JavaScript, using the addEventListener() method:

object.addEventListener("load", scriptFile);

Example: Using the addEventListener() method

HTML




<center>

    <img src

object.onload = function(){scriptFile};
0

object.onload = function(){scriptFile};

object.onload = function(){scriptFile};
2
object.onload = function(){scriptFile};
3
object.onload = function(){scriptFile};
0
object.onload = function(){scriptFile};
5
object.onload = function(){scriptFile};
6
object.onload = function(){scriptFile};
0
object.onload = function(){scriptFile};
8>

    <

object.addEventListener("load", scriptFile);
2
object.onload = function(){scriptFile};
3
object.onload = function(){scriptFile};
0
object.addEventListener("load", scriptFile);
5
object.addEventListener("load", scriptFile);
6
object.addEventListener("load", scriptFile);
2>

object.addEventListener("load", scriptFile);

    <<2>

<4<5

<4<7

<8<9

<4center1

    center3<2>

center3center>

Output: Here, the getElementById() method will return the elements that have given an ID which is passed to the function. It can also be used to change the value of any particular element or get a particular element. The DOM innerHTML property will be used to set or return the HTML content of an element.

Can we use onload in div?

HTML DOM onload Event

We have a complete list of HTML DOM methods, to check those please go through this HTML DOM Object Complete reference article.

Supported tags:

  • <body>: It is used to define the main content present inside an HTML page.
  • <frame>: It is used to divide the web browser window into multiple sections where each section can be loaded separately.
  • <iframe>: It is used to define a rectangular region within the document in which the browser can display a separate document, including scrollbars and borders.
  • <img>: It is used to add images inside a webpage/website.
  • <input type=”image”>: It is used to specify the type of <input> element to display.
  • <link>: It is used to define a link between a document and an external resource.
  • <script>: It is used to define the client-side script.
  • : It is used to modify our text, viewed in the page.

Supported Browsers: The browsers supported by HTML DOM onload Event are listed below:

  • Google Chrome 1.0
  • Internet Explorer 9.0
  • Microsoft Edge 12.0
  • Firefox 1.0
  • Safari 3.0
  • Opera 9.0

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

Where can I use onload?

onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.). However, it can be used on other elements as well (see "Supported HTML tags" below).

Which elements support onload?

onload is an event specific to the body , frame , iframe , img , link , and script elements.

Is there an onload function in JavaScript?

The onload in Javascript is responsible for loading objects within a web page, it occurs when the web page is loaded and needs the loading of other important elements. The onload in javascript is compatible with loading elements like CSS files, images, script files, etc. within a web page.

Is onload necessary?

In HTML, the onload attribute is generally used with the <body> element to execute a script once the content (including CSS files, images, scripts, etc.) of the webpage is completely loaded. It is not necessary to use it only with <body> tag, as it can be used with other HTML elements.