'Activating' JavaScript for each page in large web sites
<p>I am working on a medium size web site that has plenty of custom JavaScript written for it.</p>

<p>At present all the script is stored in seperate JS files for each area of functionality. These are then minified and combined into a single, large JS file during our build process.</p>

<p>For each page, the relevant JavaScript is usually executed based on the presence of an element with a particular class on the page. For example:</p>

<pre><code>$(document).ready(function()
{
    var foo = $("div.dostufftome")
    if(foo)
    {
         // dostuff
    }
}
</code></pre>

<p>I'm concerned that this approach seems a little fragile, and also potentially quite slow.</p>

<p>The only other alternative I can see is to put the 'activation' code inline in the HTML in a CData section, with the bulk of the code in the attached JS file.</p>

<p>Grateful for any advice.</p>
