-
Notifications
You must be signed in to change notification settings - Fork 5
Inline script
janjongboom edited this page Aug 29, 2011
·
1 revision
Because Moth's Javascript engine encourages you to put javascript files at the bottom of the page, Moth includes a HTML helper to put inline javascript that you declare in your (partial) views, at the bottom of the page; after you have registered 3rd party libraries. Even better: the javascript is automatically minified.
For example, in your masterpage you have registered jQuery and rendered the requests at the bottom of your page (see Javascript). In one of your partial views you want to write some code that needs jQuery:
// this code will fail because jQuery wasn't loaded yet
$(function () {
$('p').addClass('yellow');
});
If you wrap your code in a Html.BeginScript
tag, you can render this piece of script at the bottom of your page:
<% using(Html.BeginScript(ScriptPosition.EndOfPage)) { %>
$(function () {
$('p').addClass('yellow');
});
<% } %>
Now the piece of code will render just fine. Best of all it's also minified!