7

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

If I put $(document).ready() in the head in script element which take a callback function that using a functions declared in a file which its script element loaded in the body like that :

<!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 a right way to do that and guarantee that the helloFuncDeclaration.js will be loaded before calling the function hello() ?

faressoft
  • 19,053
  • 44
  • 104
  • 146

3 Answers3

4

To be sure, use window onload handler:

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

Or use it like this:

<script onload="hello()" src="http://somewhere/helloFuncDeclaration.js" type="text/javascript"></script>
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • But it is called after loading all images too, Do you think it is a good idea to use load !!! – faressoft Jun 07 '13 at 12:17
  • The good idea would be to call hello() function inside helloFuncDeclaration.js file after declaration or use the onload event of this script tag – A. Wolff Jun 07 '13 at 12:19
  • helloFuncDeclaration.js maybe some remote library and I want to calling the function or declaring some objects in my own code block... – faressoft Jun 07 '13 at 12:22
  • I have used $(document).ready() for my own custom jquery code. And for all advertisement scripts (like Google AdSense or other providers) I activated them using $(window).load(), which works well! – basZero Mar 30 '15 at 14:48
2

To be sure , you can use window.load

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

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

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
1

$(document).ready() runs after all assets were loaded, so - yes

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Thelambofgoat
  • 609
  • 1
  • 11
  • 24
  • 15
    it runs after DOM is ready, so no – A. Wolff Jun 07 '13 at 12:12
  • 4
    I don't understand why this answer was down voted. http://stackoverflow.com/questions/2397534/how-soon-will-jquerydocument-ready-be-called and http://stackoverflow.com/questions/6195257/does-ready-fire-before-or-after-all-inline-scripts-have-been-loaded clearly state that `script` inside the body will always be loaded before the ready event fires. – Jonwd Nov 09 '14 at 03:26
  • No, it's called after DOM elements are downloaded not all assets. – HieuHT Aug 04 '16 at 07:24
  • That means scripts inside the body tag. Optimally you do not have scripts inside the body tag. – Shayne Aug 18 '16 at 02:07
  • this answer is wrong and it could lead to bugs in JS. document.ready event do not means, that all JS from body was loaded. – Andrew Zhilin Jun 04 '19 at 10:15