Firstly, the best approach is to externalize JavaScript from the controls in a JS file that loads on the first request to your site and in all subsequent requests (e.g. site.js). This file will then be cached by the browser, and users will not have to download JS again and again, as if JS remained in the controls.
Then you need to create a mechanism to execute only those parts of the script that are relevant to the executable page or control.
There are several ways to achieve this, however, if I were in your situation, I would do it this way.
Define a page-level element that wraps most of your content ... usually people call it wrapper . Dynamically add a custom attribute to the wrapper called " data-js ". Set the value of "data-js" dynamically (server side) from any page or user / user control. Let it have several keys separated by a pipe (|) or another character.
<div class="wrapper" data-js="funtion1|function2"> </div>
Use jQuery to search for this attribute in the DOM and execute JS custom functions based on the value in the data-js attribute. This function will only contain code that is relevant to the current page or controls.
$(function(){ var funcKeys = $('.wrapper').attr('data-js'); if(funcKeys != null) { var funcs = funcKeys.split('|'); for(var i=0; i< funcs.length; i++) { var funcName = eval(funcs[i]); if(typeof funcName == 'function') { funcName(); } } } });
Using this approach, you only need the jQuery CDN link, the link to your site's JS file, and the above code snippet in your layout.
Then you just need to include certain page / control functions in your JS file of your site, for example:
function function1() { alert("This is function 1"); } function function2() { alert("This is function 2"); }
Hope this helps.
FYI, I am also setting up the fiddle so you can play with it: http://jsfiddle.net/hh84f/1/