Page-level execution of JavaScript when serving concatenated files
<h3>Scenario:</h3>

<p>A web site with <em>x</em> number of pages is being served with a <strong>single, concatenated JavaScript file</strong>. Some of the individual JavaScript files pertain to a page, others to plugins/extensions etc.</p>

<p>When a page is served, the entire set of JavaScript is executed (as execution is performed when loaded). Unfortunately, <strong>only a sub-section of the JavaScript</strong> pertains directly to the page. The rest is relevant to other pages on the site, <strong>and may have potential side-effects on the current page if written poorly</strong>.</p>

<h3>Question:</h3>

<p><strong>What is the best strategy to only execute JavaScript that relates directly to the page, while maintaining a single concatenated file?</strong></p>

<h3>Current solution that doesn't feel right:</h3>

<p>JavaScript related to a specific page is wrapped in a "namespaced" <em>init</em> function for that page. Each page is rendered with an inline script calling the init function for that page. It works hunky-dory, but <strong>I would rather not have any inline scripts</strong>. </p>

<p>Does anyone have any clever suggestions? Should I just use an inline script and be done with it? I'm surprised this isn't more of an issue for most developers out there.</p>
