@Nicolas - I'm curious as to why you find Ricardo's easiest?

Ricardo's first solution would work, but to my mind adds extra
complexity. You're adding a dependency on the presence of elements in
the markup to execute your functions. If you work, as I currently do,
in a multi-developer environment, it's very easy for someone to change
the id of the element you're using, or perhaps remove it completely,
which would immediately stop the functions from being called. They
could also add that element to a page using a different identifier,
and now that page would match two of Ricardo's if statements, calling
functions that won't exist and so you're back to the original issue.
This latter condition means you have to make sure you have a unique
identifier element for every page, or follow Ricardo's second
suggestion and use a unique class on the body tag of every page. One
thing not apparent in his example is that the user may want multiple
functions to run on each page, for example:

page 1: functions 1,2,& 4
page 2: functions 3,4 & 6
etc.

This increases the complexity of the if statements and which functions
go where, and seems more likely to break over time as functions get
added or removed.

My suggestion of just keeping an array of functions in one location
(the external js), and checking for the presence of the function on
domready and executing it if found, rather than a 'middle-man' element
that has to stand in for one or more functions, seems much more
direct--to me at least :)

Maybe I'm missing something that makes Ricardo's seem to be easiest?


On Feb 9, 2:26 am, Nicolas R <ruda...@googlemail.com> wrote:
> I not sure if this suits you, but you could split your functions to
> separate files and then lazy load each js file as necessary.
> In such casehttp://nicolas.rudas.info/jQuery/getPlugin/may be
> helpful
>
> Otherwise I find Ricardo's suggestion the easiest. You could also do
> some time tests to check whethercallingthese functions when not
> really needed effects performance, and act accordingly
>
> On Feb 9, 3:33 am, mkmanning <michaell...@gmail.com> wrote:
>
> > *Tab+spacebar and it posts :P
>
> > You could put your list of functions in an array in your external js,
> > then call them on the window object in a loop:
> > $(function() {
> >         var funcs = [
> >         'ManageCategoriesClick',
> >         'HideByDefault',
> >         'PrepareSplitForm',
> >         'SetUpAdvertPopup',
> >         'CheckAll',
> >         'CheckNone',
> >         'EditCategoryInPlace',
> >         'PreparePaneLinks',
> >         'PrepareToolTips'
> >         ]
>
> >         $.each(funcs,function(i,f){
> >                 if(typeof(window[f]) == 'function'){
> >                         window[f]();
> >                 }
> >         });
> >  });
>
> > On Feb 8, 5:21 am, Beau <zar...@gmail.com> wrote:
>
> > > Thanks for the ideas everyone!
>
> > > @Stephan: Yes, it's in an external JS file. I'd prefer to not have to
> > > do any inline javascript. I've considered it, but thanks for the
> > > suggestion!
>
> > > @Ricardo: Thanks for those. I may end up doing a variation of them.
>
> > > On Feb 8, 4:50 am, Stephan Veigl <stephan.ve...@gmail.com> wrote:
>
> > > > Hi
>
> > > > I guess you have your $().ready()functionin an external js file,
> > > > otherwise you could
> > > > customize it for the according html page.
>
> > > > Another construct similar to Ricardos one, but a bit more flexible:
>
> > > > Use a global variable in every html file to specify the init functions
> > > > you want to call for this page:
> > > > <script type="text/javascript">
> > > >         myInitFxn = [ManageCategoriesClick, HideByDefault, 
> > > > PrepareSplitForm,...];
> > > > </script>
>
> > > > ready.js:
> > > > $().ready(function(){
> > > >         for(var i in myInitFxn) {
> > > >                 myInitFxn[i](); // call initfunction
> > > >         }
>
> > > > });
>
> > > > by(e)
> > > > Stephan
>
> > > > 2009/2/8 brian <bally.z...@gmail.com>:
>
> > > > > On Sat, Feb 7, 2009 at 11:21 PM, Ricardo Tomasi 
> > > > > <ricardob...@gmail.com> wrote:
>
> > > > >> Alternatively you could add a different class to the body of each
> > > > >> page, then use this rather amusing construct:
>
> > > > >> $(document).ready((function(){
> > > > >>  var is =function(v){ return ++document.body.className.indexOf(v) };
>
> > > > >>  return(
> > > > >>   is('categories')
> > > > >>     ? ManageCategoriesClick :
> > > > >>   is('hidebydefault')
> > > > >>     ? HideByDefault :
> > > > >>   is('form')
> > > > >>     ? PrepareSplitForm :
> > > > >>   is('advert')
> > > > >>     ? SetUpAdvertPopup :
> > > > >>  function(){} //nothing
> > > > >>  );
>
> > > > >> })());
>
> > > > > That is, indeed, amusing. And one for my toy chest. Thanks!
>
> > > > > Who knew, back in '96, that javascript was going to turn out to be so 
> > > > > much fun?

Reply via email to