views:

743

answers:

3

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.

A: 

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($){

});
CodeJoust
read question again pls, it ask something else :)
Basit
Sorry, no it wouldn't slow anything down...
CodeJoust
+1  A: 

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.

SLaks
thank you, but if you read my whole thread.. i do know about (function and replacing $ to jQuery.. :P
Basit
+4  A: 

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.

karim79
I agree with this in that I don't see how this could (or should) make the page slow. I do this myself all the time, sometimes just for the sake of scoping and keeping the global space clean. However, despite what I think _should_ make sense, I think the only real way to feel confident in this, is to actually test it and gather the actual metrics. With that said, I've never noticed any problems with the way that I do this...
Funka
only one question is left, which i need to know. i need my functions in another $().ready method too, how can i make it compatiable so it works on both places, right now is only working as @karim79 defined
Basit
@basit - The only way is to declare them outside of document.ready, which you've already stated you *can't* do because of mootools, so honestly I don't know what to suggest, so I think something's gotta give :) !
karim79
i guess $j would do it lol.. i was really hoping for some other way, then changing my code on every place. if you guys think still there is some way, then please do share, else thank you very much :)
Basit