This question is locked. New answers and comments are not allowed.
How would you go about adding inline JavaScript using the ScriptRegistrar?
I realize it is desirable to place JavaScript in external files, but there are scenarios where server code needs to dynamically assign a value to a JavaScript variable.
Take for this example. Say I have a custom component implemented as a partial view. I want to keep all of my related code together. How would I ensure my inline JavaScript gets appended to the end of the HTML document and after the <script> elements rendered by the ScriptRegistrar? (Assume the inline JavaScript has a dependency on an external script rendered by the ScriptRegistar.)
Example Code:
Added June 15, 2010
One workaround I thought of right after I posted the above was to simply store any server-side generated values in a hidden form field and then let your JavaScript code (called from an external file) have a dependency on the hidden form field.
However I think it would be good to also have the option to inline the JavaScript and have it render at the end of the HTML document with the rest of the script elements.
Added August 30, 2010
It seems we can use the ScriptRegistrarBuilder.OnDocumentReady(string) method to inline any JavaScript we would want executed after the page loads.
Instead of inlining the script block we could just initialize a user-defined, global JavaScript object or variable that was defined in a .js already loaded.
I realize it is desirable to place JavaScript in external files, but there are scenarios where server code needs to dynamically assign a value to a JavaScript variable.
Take for this example. Say I have a custom component implemented as a partial view. I want to keep all of my related code together. How would I ensure my inline JavaScript gets appended to the end of the HTML document and after the <script> elements rendered by the ScriptRegistrar? (Assume the inline JavaScript has a dependency on an external script rendered by the ScriptRegistar.)
Example Code:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %> |
<%: Html.MyCustomComponent %> |
<script type="text/javascript"> |
var MyDynamicVariable = '<%: Model.MyDynamicVariable %>'; |
DoSomething(MyDynamicVariable); |
</script> |
Added June 15, 2010
One workaround I thought of right after I posted the above was to simply store any server-side generated values in a hidden form field and then let your JavaScript code (called from an external file) have a dependency on the hidden form field.
However I think it would be good to also have the option to inline the JavaScript and have it render at the end of the HTML document with the rest of the script elements.
Added August 30, 2010
It seems we can use the ScriptRegistrarBuilder.OnDocumentReady(string) method to inline any JavaScript we would want executed after the page loads.
Instead of inlining the script block we could just initialize a user-defined, global JavaScript object or variable that was defined in a .js already loaded.