If Statement Funkiness

a place to get help

Moderator: 1.1 Moderators

If Statement Funkiness

Postby josh.beck on Mon Sep 29, 2008 2:47 am

Hey All,

I was happy to find this forum, I didn't know there was a new one since the site update at Moo.

I have some strangeness that I'm wondering if someone can help with.

I have my js functions separated into 2 functions in my control file (which holds all the js calls to mootools) as follows:

function pageVariables ()

and

function loadVariables ()

then I implement these functions accordingly via window.addEvent ('domready', pageVariables) etc.

It makes a nice easy to understand set up where I can simply nest functions inside one or the other to ensure they are called at the right time.

As part of this, since all functions become available on either load or domready I've always used "if" statements to stop errors since a given object may not appear on a given page and would produce an error.

For example:

  1.  
  2. if ($$('a.myClass')) {
  3.    $$('a.myClass').each (function (el) {
  4.       el.addEvent ('click', function (e) {
  5.         e = new Event (e).stop ();
  6.         // do something here
  7.       });
  8.    });
  9. }
  10.  


This way the code within will only execute if there's a link with the class of myClass on the page.

This has always worked perfectly till today :(

Is there some reason this would be failing? or perhaps a better or more elegant way to do the same thing?

Thanks

Josh
josh.beck
 
Posts: 3
Joined: Mon Sep 29, 2008 2:28 am

Re: If Statement Funkiness

Postby slosd on Mon Sep 29, 2008 9:21 am

From the docs:
If an expression doesn't find any elements, an empty array will be returned.


You actually can remove the if condition:
  1.  
  2.    $$('a.myClass').each (function (el) {
  3.       el.addEvent ('click', function (e) {
  4.         e = new Event (e).stop ();
  5.         // do something here
  6.       });
  7.    });
  8.  
User avatar
slosd
mootools fan
 
Posts: 50
Joined: Sun Aug 03, 2008 8:56 pm

Re: If Statement Funkiness

Postby josh.beck on Mon Sep 29, 2008 9:51 am

Right,

But that's the whole purpose of putting the expression into the if statement so that the empty array isn't thrown at all.

Let me give you a working example.

I have 2 pages for instance active.html and error.html

On the active.html page I want to implement some MultiBox links that will display my terms, privacy etc.

On the error.html page I don't want these to open at all, so I use a .stop() action by class selector.

So, no MultiBox links mean no need to add the excess overhead of loading multibox.js.

My code in the pageVariable function looks like so:

  1.  
  2.         if ($$('a.mb')) {
  3.         new MultiBox('mb', {
  4.             useOverlay: true,
  5.             showControls: false,
  6.             openFromLink: false
  7.         });
  8.     }
  9.    
  10.     /* Kill Footer Links on Error Pages */
  11.    
  12.     if ($$('a.da')) {
  13.         $$('a.da').each (function (el) {
  14.             el.addEvent ('click', function (e) {
  15.                 new Event (e).stop ();                             
  16.             });
  17.         });
  18.     }
  19.  


The objective being obviously to allow the MultiBox object to be implemented ONLY when the .mb links appear on a page, and to kill those links when they're class equals .da

But, this doesn't work, it throws an error on the error.html page saying that MultiBox is not defined.

Hope that makes sense :)

Josh
josh.beck
 
Posts: 3
Joined: Mon Sep 29, 2008 2:28 am

Re: If Statement Funkiness

Postby slosd on Mon Sep 29, 2008 9:57 am

Of course you can only remove the if condition if you use the elements from $$() in a loop.
  1.  
  2.         if ($$('a.mb').length > 0) {
  3.         new MultiBox('mb', {
  4.             useOverlay: true,
  5.             showControls: false,
  6.             openFromLink: false
  7.         });
  8.     }
  9.    
  10.     /* Kill Footer Links on Error Pages */
  11.  
  12.         $$('a.da').each (function (el) {
  13.             el.addEvent ('click', function (e) {
  14.                 new Event (e).stop ();                            
  15.             });
  16.         });
  17.  
User avatar
slosd
mootools fan
 
Posts: 50
Joined: Sun Aug 03, 2008 8:56 pm

Re: If Statement Funkiness

Postby josh.beck on Mon Sep 29, 2008 10:39 am

Ah, see ... there's always a simple solution :)

Curious though that I haven't run into this before now, probably just a change in my technique. I used to load everything, compress it and cache it. I'm trying now to get away from that and load only what's absolutely necessary and ONLY when it's needed.

Anyway, thanks a bunch, I really appreciate the help :)

Josh
josh.beck
 
Posts: 3
Joined: Mon Sep 29, 2008 2:28 am


Return to Help

Who is online

Users browsing this forum: MSN [Bot] and 1 guest