I'm working with an application that uses way more JQuery that I've dealt with before and I'm trying to understand what JQuery document.ready() does to a web application a little better. I'm hoping that someone more experienced in JS/JQuery could help me out.
Let's assume I have an separate .js file with 100 JQuery functions inside a document.ready():
$(document).ready(function() {
$("#something1").click(function() { ... });
$("#something2").click(function() { ... });
$("#something3").click(function() { ... });
etc...
});
I understand that these would now be loaded and ready for every page of the website (through the inclusion of the .js file). However, I could also stick these inside separate document.ready() functions on every individual page of the website, where each page would only get what it actually uses. Or I could craft something even more sophisticated by selectively calling functions that group event handlers inside the document.ready();
Given that the entire .js file is in any case read by the browser, I would like to know what kind of effect there might be between these approaches on performance. It seems counter-intuitive to load all the event handlers for every page but at the same time this has me wondering whether I'm creating a problem out of something that actually isn't.
A little outside perspective would be useful. Thanks.
What is $ (document).ready () in jQuery? $ (document).ready () is an event in jQuery that is fired only when the whole DOM is fully loaded and ready to be manipulated by jQuery. This document.ready event is to prevent any jQuery code from running before the document is finished loading (is ready).
.ready( handler )Returns: jQuery. Description: Specify a function to execute when the DOM is fully loaded. Type: Function() A function to execute after the DOM is ready. The .ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate.
A page can't be manipulated safely until the document is ready. jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ).ready() method will run once the page DOM is ready to execute JavaScript code.
The best overall result is often achieved by creating a library of code that can be made available to all your pages in one minified external JS file. Then, put page-specific initialize code either inline with the page or in an external JS file specifically for that page.
This has the following advantages:
For further optimizations in a large project, if the external JS file is very large and you have a large classification of functions that are only needed in one part of the site, then you could break those into another external JS file that is only included in that part of the site. Because these files are cached effectively by the browser, the main savings is in interpreter parsing time and memory footprint which may or may not be significant (it depends on how large the project is). There is usually a practical limit to these optimizations because loading several external JS files usually takes more time than loading one larger one.
Be sure to appropriately version number your external JS files, so when you revise them the filename changes so that browsers are forced to fetch the new version and don't get stuck on the old version from cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With