# Rendering Forms Scripts

Forms output some JavaScript which is by default rendered right below the markup.

In many cases, you might prefer rendering your scripts at the bottom of the page. For example, before the closing `</body>` tag. This generally improves site performance.

In order to render your scripts where you want, you need to add a snippet to your template. Make sure you add it below your scripts, right before the closing `</body>` tag.

By default, Forms uses `HttpContext.Items` for tracking the forms rendered on a page. The stored values are used when rendering the form scripts and associated data.

The following snippet should be used.

```csharp
@if (Context.Items.TryGetValue("UmbracoForms", out object? formIdsObject) && formIdsObject is IEnumerable<Guid> formIds)
{
    foreach (var formId in formIds)
    {
        @await Component.InvokeAsync("RenderFormScripts", new { formId, theme = "default" })
    }
}
```

If you have changed the configuration value of `TrackRenderedFormsStorageMethod` to use the legacy behavior `TempData`, the snippet is:

```csharp
@using Umbraco.Forms.Web.Extensions;

@if (TempData.Get<Guid[]>("UmbracoForms") is Guid[] formIds)
{
    foreach (var formId in formIds)
    {
        @await Component.InvokeAsync("RenderFormScripts", new { formId, theme = "default" })
    }

    TempData.Remove("UmbracoForms");
}
```

Read more about the `TrackRenderedFormsStorageMethod` configuration option in the [Configuration](/umbraco-forms/developer/configuration.md#TrackRenderedFormsStorageMethod) article.

If you prefer to use a tag helper, that's an option too.

Firstly, in your `_ViewImports.cshtml` file, ensure you have a reference to the Umbraco Forms tag helpers with:

```cshtml
@addTagHelper *, Umbraco.Forms.Web
```

Then instead of reading from `TempData` and invoking the view component directly, you can use:

```cshtml
<umb-forms-render-scripts theme="default" />
```

This will use the appropriate storage method that you have configured.

## Using Form Scripts Alongside Validation Dependencies

When setting up templates for Umbraco Forms, two separate script-rendering methods are involved, and both are required for forms to work correctly.

`@Html.RenderUmbracoFormDependencies(Url)`, covered in the [Preparing Your Frontend](https://github.com/umbraco/UmbracoDocs/blob/main/umbraco-forms/developer/prepping-frontend/README.md) article, renders client-side validation scripts such as jQuery Validate and unobtrusive validation. This goes in the `<head>` of your template.

The `<umb-forms-render-scripts />` tag helper (or the equivalent view component calls shown above) renders form-specific scripts covering conditional field logic, field behaviors, and any theme JavaScript. This goes before the closing `</body>` tag.

Because both methods output `<script>` tags, they can appear to duplicate each other. The script contents are entirely different, however, and serve distinct purposes. A complete template uses both:

```cshtml
<head>
    @Html.RenderUmbracoFormDependencies(Url)
</head>
<body>
    @await Component.InvokeAsync("RenderForm", new { formId = Model.FormGuid })
    <umb-forms-render-scripts theme="default" />
</body>
```

Omitting `RenderUmbracoFormDependencies` will break client-side validation. Omitting `<umb-forms-render-scripts />` will break conditional fields and theme behaviour.

## Enabling `ExcludeScripts`

If you do not want to render the associated scripts with a Form, you need to explicitly say so. You need to make sure `ExcludeScripts` is checked/enabled, whether you are inserting your Form using a macro or adding it directly in your template.

To enable `ExcludeScripts`:

* Using the **Insert Form with Theme** macro:

  ![Exclude scripts](/files/YmtMjvfmc8cR0HGGShh0)
* While inserting Forms **directly** in your template:

  ```csharp
  @await Umbraco.RenderMacroAsync("renderUmbracoForm", new {FormGuid="6c3f053c-1774-43fa-ad95-710a01d9cd12", FormTheme="bootstrap3-horizontal", ExcludeScripts="1"})
  ```

{% hint style="info" %}
`ExcludeScripts = "1"` prevents the associated scripts from being rendered. Any other value, an empty value, or if the parameter is excluded, will render the scripts on the Form.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.umbraco.com/umbraco-forms/developer/rendering-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
