Execute a function after full page load

1

I have a page that loads several tables, then organizes them into tabs.

Would you like to perform a function when you have finished loading the page?

    
asked by anonymous 02.05.2018 / 19:42

3 answers

4

You can use the event $(window).on("load"... :

$(window).on("load", function(){
   // página totalmente carregada (DOM, imagens etc.)
});

It is quite different from $(document).ready(function()... , which only fires when the DOM has been loaded, but there may be asynchronous elements (images, scripts) still being loaded.

  

Note that the .load() method for the load JavaScript event has become obsolete in jQuery   1.8 and removed from 3.0. ( See documentation ). Use .load() only as shorthand of Ajax in jQuery ( see documentation ).

    
02.05.2018 / 19:53
1
window.onload = function () { alert("Está carregado!") } 
  • By default, it fires when the entire page loads, including its contents (images, css, scripts, etc.)
  • In some browsers, it now assumes the role of document.onload and is triggered when the DOM is also ready.
  

window.onload seems to be the most widely supported. In fact, some of the more modern browsers, in a sense, replaced document.onload with window.onload . Browser support issues are probably the reason why many people are beginning to use libraries like jQuery to handle verifying that the document is ready as follows:

$(document).ready(function() { /* code here */ }); $(function() { /* code here */ });

Source brothers site

    
02.05.2018 / 19:56
1

There are two ways to load a function after page load, it can be by the <body> tag or by jquery

See for example:

<body onLoad="nome_função();">

With this method, after loading everything inside the <body> tag, it will run the function.

The second method would be this:

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

$(window).on("load", function(){
   //seu script aqui
})

</script> 

Then you specify in the src of the first script I quoted, where your jquery file is (or use the direct link to the file).

    
02.05.2018 / 19:52