My application's front-end currently uses a single merged/minified javascript file for performance reasons. It's a convenient setup, but I have run into a problem. Individual pages have their own startup code in $(function()), but because the site's combined javascript is referenced on every page, all of the startup functions execute on every page, which is completely unintended.
The only solution I have come up is to place a conditional in each startup function to execute only if certain pieces of data can be found in the page's container. For example:
$(function() {
if($('title').text()=='some title') {
// do something
}
}
The above function would run only if the page's title is 'some title', thus preventing it from running on other pages (assuming they have different titles, of course). This strategy would likely work for the entire site, but it seems hacky and unsafe. Does anyone have a better solution?