Skip to content

Commit 73eedb9

Browse files
authored
Scripts loaded via <head> content (#34503)
1 parent 2abf7db commit 73eedb9

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

aspnetcore/blazor/javascript-interoperability/location-of-javascript.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ Loading JS from the `<head>` isn't the best approach for the following reasons:
8686
* JS interop may fail if the script depends on Blazor. We recommend loading scripts using one of the other approaches, not via the `<head>` markup.
8787
* The page may become interactive slower due to the time it takes to parse the JS in the script.
8888

89+
:::moniker range=">= aspnetcore-8.0"
90+
91+
In component markup, scripts can be loaded via a [`HeadContent` component](xref:blazor/components/control-head-content) with the usual caveat that the approach slows down page load on the client, which we recommend avoiding. When a script is loaded with a `HeadContent` component in a Blazor Server app, Blazor WebAssembly app, or a Blazor Web App using an interactive render mode (interactive SSR, CSR), navigating away from the component's page removes the `<script>` tag from the rendered `<head>` content but doesn't unload the script's JavaScript code, including event handlers that the script registers, exposed variables, and methods that the script provides. Only Blazor Web Apps using static SSR unload JavaScript code when the user navigates away from the page. Generally, you're better off adding `<script>` tags to the physical `<head>` content, unless you explicitly desire to keep such script references in the components that use them and don't mind that the code isn't unloaded on navigation events.
92+
93+
:::moniker-end
94+
95+
:::moniker range=">= aspnetcore-6.0 < aspnetcore-8.0"
96+
97+
In component markup, scripts can be loaded via a [`HeadContent` component](xref:blazor/components/control-head-content) with the usual caveat that the approach slows down page load on the client, which we recommend avoiding. When a script is loaded with a `HeadContent` component, navigating away from the component's page removes the `<script>` tag from the rendered `<head>` content but doesn't unload the script's JavaScript code, including event handlers that the script registers, exposed variables, and methods that the script provides. Generally, you're better off adding `<script>` tags to the physical `<head>` content, unless you explicitly desire to keep such script references in the components that use them and don't mind that the code isn't unloaded on navigation events.
98+
99+
:::moniker-end
100+
89101
## Load a script in `<body>` markup
90102

91103
Place the JavaScript tags (`<script>...</script>`) inside the [closing `</body>` element](xref:blazor/project-structure#location-of-head-and-body-content) after the Blazor script reference:

0 commit comments

Comments
 (0)