/// <summary> /// Registers inline JavaScript "inclusion" for your page. /// /// Notice, a JavaScript "inclusion", is an inline, persistently included piece of JavaScript, which if in the rare occasion of a /// normal PostBack should occur, will be automatically re-included for you, during the rendering of the HTML for your PostBack. /// This allows you to "include" JavaScript, as if it was contained in a static file. /// In addition, it will make sure that the same JavaScript object, is not included twice. /// /// Notice, if you only want to "send" some JavaScript to the client, and not persistently "include" it, you should rather use the /// "SendJavaScriptToClient" method. This is useful for sending JavaScript to the client, that only should be evaluated once, and not /// persistently included. Examples include for instance to set focus to some widget, in response to some event occurring, etc. /// The latter will not check to see if your JavaScript object has been previously sent to the client. /// </summary> /// <param name="script">JavaScript to register for inclusion and evaluation on client</param> public void IncludeJavaScriptObject(string script) { // Checking if specified JavaScript inline "inclusion" is already included on page. if (!PersistentJSInclusions.Exists(ix => !ix.Item2 && ix.Item1 == script)) { // JavaScript inclusion has not been previously included, making sure we store it in ViewState, to avoid redundant inclusions in // the future, before we register it for a "push" to client. var jsObject = new Tuple <string, bool> (script, false /* Registering the fact that this is an object */); PersistentJSInclusions.Add(jsObject); _javaScriptObjectsForCurrentRequest.Add(jsObject); } }
/// <summary> /// Registers JavaScript files for inclusion on your page. /// /// Will automatically keep track of all previously included JavaScript files, to avoid redundant inclusions, /// and even include JavaScript files during Ajax requests automatically for you, making sure they're included on the client-side. /// Notice, if you wish, you can supply an absolute URL, to include files from for instance a CDN, or similar types of sources. /// You can also use "ClientScript.GetWebResourceUrl" to include JavaScript files that are embedded resources in other projects. /// </summary> /// <param name="url">URL to JavaScript to register</param> public void IncludeJavaScriptFile(string url) { // Checking if specified file is already included. if (!PersistentJSInclusions.Exists(ix => ix.Item2 && ix.Item1 == url)) { // File has not been previously included, making sure we store it in ViewState, to avoid redundant inclusions in the future, // before we register it for a "push" to client. var file = new Tuple <string, bool> (url, true /* Registering the fact that this is a file */); PersistentJSInclusions.Add(file); _javaScriptObjectsForCurrentRequest.Add(file); } }