Reputation: 121
I have a small issue in my project. Our project includes many jsps having bunch of javascript and $(document).ready with them and on executing all the jsps are combined. So we have no idea which function present in the whole array of $(document).ready will be executed in the beginning.(I know, careless from my part). Is there any way to call a function in Jquery or javascript which will execute before any of the other functions in the wide list of $(document).ready functions. This issue has come up because we were asked to add a loader as soon as the document is ready to make the users know that the page is not fully loaded. Please let me know if there are any solutions.
Thanks and regards, Anil Simon
Upvotes: 1
Views: 632
Reputation: 17
Deffered function will resolve your issues
$.when( $.getScript(url), $.ready ).done(function(){
// update your code here
});
Upvotes: 1
Reputation: 6702
Any script in the HTML page is executed immediatly. Scripts should be placed at the end of the body, if no reason indicates something else. The jQuery ready handler as well as the native DOMContentReady event is fired after the complete HTML tree has been loaded, so any code in the HTML will be executed before. The DOM so far is already accessible.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script>
$(function(){alert('jq doc ready');});
</script>
</head>
<body>
Page Content
<script>
// script is executed immediately
// no need to put it into a function - anyway... e.g. for encapsulation
(function()
{
alert('not yet loaded'); //executed before DOM ready
})();
</script>
</body>
</html>
Upvotes: 1
Reputation: 172
Holds or releases the execution of jQuery's ready event.
$.holdReady(true);
$.getScript("myplugin.js", function() {
$.holdReady(false);
});
Upvotes: 2