Is $ (document) .ready () called after loading all script files into the body?

Is $ (document) .ready () called after loading script js files in the body?

If I put $ (document) .ready () in the head in a script element that accepts a callback function that uses the functions declared in the file, its script element is loaded into the body as follows:

<!DOCTYPE HTML> <html> <script src="jquery.js" type="text/javascript"></script> <script> $(function(){ hello(); }) </script> <head> </head> <body> <script src="http://somewhere/helloFuncDeclaration.js" type="text/javascript"></script> </body> </html> 

Is it right to do this and guarantee that helloFuncDeclaration.js will be loaded before calling the hello () function?

+4
source share
3 answers

Of course, use the onload handler:

 $(window).on('load', hello); 

Or use it as follows:

 <script onload="hello()" src="http://somewhere/helloFuncDeclaration.js" type="text/javascript"></script> 
+4
source

$(document).ready() run after all resources are loaded, so yes

+2
source

Of course you can use window.load

 $(window).load(function(){ hello(); }) 

A load event is dispatched to an element when it and all sub-elements have been fully loaded. This event can be sent to any element associated with the URL: images, scripts, frames, iframes and window objects.

+2
source

Source: https://habr.com/ru/post/1485030/


All Articles