Search code examples
javascriptjqueryhtmlcode-organization

how to organize a little javascript in a large php website


I am working on a large php mysql CMS. I did a decent job organizing my server side code with includes, objects, etc. Now that I have started to jazz up the front end with things like form validation and image sliders I am starting to accumulate a mess of tiny javascript/jquery functions. Most of these are under 5 lines and are only used on one page each.

Is it bad practice/lazy to throw all of these into a functions.js file, include it in my header then call myJavaScriptFunction() in the page where I need it? If so what is a better approach?


Solution

  • Is it bad practice/lazy to throw all of these into a functions.js file, include it in my header then call myJavaScriptFunction() in the page where I need it?

    This would work, but be careful you're not loading large amounts of useless functions in each page. For example, if you have 30 Javascript functions related to manipulating a Google Map and you have only one page which deals with the Google Maps API, you'll be wasting a lot of time loading these useless functions if you include them on each page.