jQuery $(document).ready(); declaring all the functions in it
-
13-09-2019 - |
Question
Explanation: i have few objects and im declaring them inside $(document).ready(). WHY? because in thous objects i have many jquery methods $(..), obviously they can work outside too, but when i include mootool, then it stop working. i tried noConflict and some other things, nothing works, only if i change the $() to jQuery() or to $j().. and i dont want to change for my 20 files and more then 2000 lines for each file. anyway declaring my objects inside $(document).ready(). made them work just fine.
Now My Question is: if i declare all these objects inside the $(document).ready() method, would it make my site slow? or would it make things slow from client side? thats the only concern in my mind.
Solution
I don't see how doing that would make your site slow. By declaring them within the $().ready
you're simply restricting the scope of your declarations to that particular $().ready
function, thus they won't be available from within the scopes of other ready functions on the same page - which should not really be a bother if your application is well-designed and you've stuck to one per page.
Oh, and your declarations certainly won't have been been parsed until the DOM is fully loaded, (as you know, $().ready
only executes once the DOM has loaded), but that too should not be a problem as you're only utilizing them from within a ready function (at least I hope).
Do you really need two libraries? If it's just one or two little tidbits of functionality you are using from one of those libraries chances are you can mimic that behaviour using the one you're making the greatest use of. If you can possibly/feasibly do that it will make your life so much simpler.
OTHER TIPS
Doing everything in jQuery.ready
will not slow down your site.
As an alternative solution, you could replace $
with jQuery
in all of your jQuery code, or you could wrap it in a function like this:
(function($) {
$('whatever').something();
})(jQuery);
This code makes a function that takes a paremeter called $
, and calls that function with the jQuery
object. The $
parameter will hide mootools' global $
object within the scope of the function, allowing you to write normal jQuery code inside the function.
Just declare
jQuery.noConflict
before the document.ready
request, then alias the jQuery method to $ within in the document.ready
...
jQuery.noConflict();
jQuery(document).ready(function($){
});