Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is $(document).ready() called after loading all script files in the body? [duplicate]

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() ?

like image 329
faressoft Avatar asked Jun 07 '13 12:06

faressoft


2 Answers

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>
like image 79
A. Wolff Avatar answered Feb 04 '23 21:02

A. Wolff


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.

  • http://api.jquery.com/load-event/
like image 43
Mohammad Adil Avatar answered Feb 04 '23 21:02

Mohammad Adil