[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
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 case http://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 whether calling these 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() function in 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 init function } }); 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?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
@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?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
I think the issue is that all of the functions are declared on the same external JS file. So you can't check for the function itself, as it will always exist, you need to check for some condition in the page. All of this will be inside $(document).ready on the external file. There are many other possibilities, a meta tag, using the title or any other text/element on the page. The choice boils down to how the whole site/app is structured and development practices in use. cheers, - ricardo On Feb 9, 3:29 pm, mkmanning michaell...@gmail.com wrote: @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/maybe 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') ?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
Ah, I have misread it then; I was under the impression the calls to the function were on the external page, but the functions themselves were on separate pages. Thanks for clearing that up. On Feb 9, 9:47 am, Ricardo Tomasi ricardob...@gmail.com wrote: I think the issue is that all of the functions are declared on the same external JS file. So you can't check for the function itself, as it will always exist, you need to check for some condition in the page. All of this will be inside $(document).ready on the external file. There are many other possibilities, a meta tag, using the title or any other text/element on the page. The choice boils down to how the whole site/app is structured and development practices in use. cheers, - ricardo On Feb 9, 3:29 pm, mkmanning michaell...@gmail.com wrote: @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/maybe 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
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
Hi I guess you have your $().ready() function in 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 init function } }); 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?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
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() function in 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 init function } }); 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?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
You could also just keep the list of functions in an array in your external js file and then check the window object for them: $(function() { var funcs = [ 'ManageCategoriesClick', 'HideByDefault', 'PrepareSplitForm', 'SetUpAdvertPopup', 'CheckAll', 'CheckNone', 'EditCategoryInPlace', 'PreparePaneLinks', 'PrepareToolTips' ] $.each(funcs,function(i,f){ if(typeof(window[f]) == 'function'){ console.log('found '+f); 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() function in 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 init function } }); 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?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
*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() function in 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 init function } }); 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?
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
Checking for the presence of a relevant element should do: $(document).ready(function(){ if ( $('#categories').length ) ManageCategoriesClick(); if ( $('#ghosts').length ) HideByDefault(); if ( $('#split').length ) PrepareSplitForm(); }); 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 ); })()); (anonymous function returning the relevant function) In the second case only a single function can be returned, anyway I hope this can give you some ideas. - ricardo On Feb 7, 10:43 pm, Beau zar...@gmail.com wrote: So I have my document.ready function, looks something like this: $(document).ready(function(){ ManageCategoriesClick(); HideByDefault(); PrepareSplitForm(); SetUpAdvertPopup(); CheckAll(); CheckNone(); EditCategoryInPlace(); PreparePaneLinks(); PrepareToolTips(); }); Those functions all call things, but many are for specific pages. Does it slow the page down if they don't find the elements the functions call? What's the best way to call functions that are specific to a single page? -beau
[jQuery] Re: Does it hurt to call functions that don't do anything on the page?
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?