As most of .net web application developers know, dynamic scripts can be registered like below
if (!Page.ClientScript.IsStartupScriptRegistered(GetType(), "script"))
{
Page.ClientScript.RegisterStartupScript(GetType(), "script", _sbDynamicScript.ToString(), true);
}
where _sbDynamicScript is a instance of StringBuilder with the dynaic javascript code. I could persist this in Session and forget about the whole stuff but i thought SO has more ideas on how to do this,
my idea would be to write the script to a file inside folder in virtual directory of application hosted inside webserver(IIS). Thus can enable caching, check file modification and re-write script from file, enable minification and more. But i would like your ideas too 🙂 btw i would like to see your code on how you would write the script into file and also check for modifications to re-register the script.
If you can write javascript to the file and reuse it later on than it is not really dynamic isn’t it?
Just create a js file and link it from the page.
In most cases the only ‘dynamic’ part of javascript are ids of the controls. Such javascript can be easily rewritten as functions that take control ids as parameters. You can that store the static script in js file (minified, gzipped, versioned and cached) and only embed invocation on the pages.