Oh, now I understand. You don't just want to wait *until* the parent
container exists - that's a given - you want to run your code only *if* the
parent exists.

That's an excellent idea. Don't run code if you know you don't need it. You
will save both time and memory.

Ricardo mentioned a time of 10ms to run 100 $('#noSuchID') selectors. I got
curious and ran a test, and it was indeed about 10ms in Firefox, but over
65ms in IE7. These times would of course vary with machine speed, but
remember that many of your site visitors have slower machines than you do!

I do agree with Ricardo that there may be a better way to code this, such as
including an initialization call only in the pages that need it - but it
would be hard to suggest anything without knowing more about how your pages
are set up and built, and what the actual HTML and JS code looks like.

-Mike

> From: Micky Hulse
> 
> I guess I am still a little confused about when/if none of 
> the elements exist...
> 
> For example, let's say I have a global JS file that is 
> included on all pages of my site, but let's say 
> "#parentContainer", and all of its children, only appear on 
> the homepage (for example)... Would it add to my JS overhead 
> to have my global JS file always looking for elements that 
> only exist on the homepage (when not on the homepage)?
> 
> I was thinking I could just look for the parent and not 
> reference children until the parent exists, in order to save 
> on memory(?)
> 
> =========================================
> 
> var $parentObj = $('#parentContianer');
> if($parentObj.length > 0) {
>         // $parentObj exists, cache child objects:
>         var $childObj1 = $('#childEle1');
>         var $childObj2 = $('#childEle2');
>         var $childObj3 = $('#childEle3');
>         var $childObj4 = $('#childEle4');
>         var $childObj5 = $('#childEle5');
>         var $childObj6 = $('#childEle6');
> 
> }
> 
> =========================================
> 
> But, then again, I am the student here (still waiting for my 
> jQuery reference book to arrive in the mail). :D
> 
> Am I over-thinking this? Should I even worry about 
> referencing objects that do not exist?
> 
> For example, if my master JS file did something like this:
> 
> $(function() {
> var $obj1 = $('#foo1');
> var $obj2 = $('#foo2');
> var $obj3 = $('#foo3');
> var $obj4 = $('#foo4');
> var $obj5 = $('#foo5');
> var $obj6 = $('#foo6');
> var $obj7 = $('#foo7');
> var $obj8 = $('#foo8');
> ...
> ...
> var $obj22 = $('#foo22');
> ...
> ...
> });
> 
> Would it be sill for me to reference all those objects on 
> every page, even if I know that not every element is on all 
> pages of my site?
> 
> Pheew, I hope I am making sense here... Hrmmm, lol! Maybe I 
> have been driking too much coffee?
> 
> Well, anyway... Many many thanks for the help Mike! I greatly 
> appreciate your time. :)
> 
> Have a great day/night!
> Cheers,
> Micky
> 

Reply via email to