Documentation

Inline Scripts

Ad-hoc inline page scripts can be managed by using the AddInlineScript method.

@{
    Bundles.AddInlineScript("alert('Hello');");
}

When @Bundles.RenderScripts() is called, the following <script> will be generated:

<script type="text/javascript">
alert('Hello');
</script>

Multi-line scripts

A razor <text> tag can also be used to generate including multi-line scripts with data inserted.

@{
    var message = "Hello World!";
    Bundles.AddInlineScript(
      @<text>
        if (someTest) {
            alert('@message');
        }
      </text>);
}

Alternatively, you can use an actual <script> tag. This has the advantage of displaying Visual Studio intellisense and syntax highlighting.

@{
    var message = "Hello World!";
    Bundles.AddInlineScript(
      @<script>
        if (someTest) {
            alert('@message');
        }
      </script>);
}