Perform a function after full page loading

Asked

Viewed 13,294 times

1

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

You would be able to perform a function when all page loading is finished ?

3 answers

4


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

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

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

It is worth remembering that the method .load() for the event load Javascript has become obsolete in jQuery 1.8 and removed from 3.0. (See documentation). Use .load() only as shorthand ajax in jQuery (see documentation).

  • What’s the difference between yours and this: $(window).load(function() {})

  • Oops! In the yellow part of the answer is explained.

  • 1

    Jesus, slap me in the face ! Too blind ! rs

  • @RBZ since you ordered, take https://i.stack.Imgur.com/tY276.jpg

  • @Leocaracciolo do not know if taking the slap is worse, or be Robin ! hahahah

  • @Sam how to load with pure javascript?

  • @Tiago Seria: window.onload = function(){ // código aqui }

  • @See if you can help me: https://answall.com/questions/429850/erro-em-ao-passar-valor-para-o-input-cannot-set-property-value-of-null

  • Tries that... erases what is inside the if and in place put inputExibeNome();.. and remove onload from body.

Show 4 more comments

2

window.onload = function () { alert("Está carregado!") } 
  • By default, it is fired when the entire page is loaded, 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 most modern browsers, in a sense, have replaced document.onload for window.onload. Browser support issues are probably the reason many people are starting 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 website

1

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

Take for example:

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

With this method, after full loading of everything inside the tag <body>, 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> 

So you specify on src from the first script I mentioned, where your jquery file is (or use the direct link to the file).

  • $(window).load(function(){}) didn’t work...

  • 1

    As the dvd said up there, it has become obsolete and in the latest version of jquery has been replaced. So to replace came the: $(window). on("load", Function(){}. I forgot this remark that was well remembered by him.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.