Skip to content

Commit 5afed2e

Browse files
authored
Blazor JavaScript isolation (#19906)
1 parent 9a57cca commit 5afed2e

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

aspnetcore/blazor/call-javascript-from-dotnet.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to invoke JavaScript functions from .NET methods in Blazo
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 07/07/2020
8+
ms.date: 09/17/2020
99
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: blazor/call-javascript-from-dotnet
1111
---
@@ -94,6 +94,13 @@ Don't place a `<script>` tag in a component file because the `<script>` tag can'
9494

9595
The <xref:Microsoft.JSInterop.IJSRuntime> abstraction is asynchronous to allow for Blazor Server scenarios. If the app is a Blazor WebAssembly app and you want to invoke a JavaScript function synchronously, downcast to <xref:Microsoft.JSInterop.IJSInProcessRuntime> and call <xref:Microsoft.JSInterop.IJSInProcessRuntime.Invoke%2A> instead. We recommend that most JS interop libraries use the async APIs to ensure that the libraries are available in all scenarios.
9696

97+
::: moniker range=">= aspnetcore-5.0"
98+
99+
> [!NOTE]
100+
> To enable JavaScript isolation in standard [JavaScript modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules), see the [Blazor JavaScript isolation and object references](#blazor-javascript-isolation-and-object-references) section.
101+
102+
::: moniker-end
103+
97104
The sample app includes a component to demonstrate JS interop. The component:
98105

99106
* Receives user input via a JavaScript prompt.
@@ -470,6 +477,43 @@ For more information, see the following issues:
470477
* [Circular references are not supported, take two (dotnet/aspnetcore #20525)](https://github.com/dotnet/aspnetcore/issues/20525)
471478
* [Proposal: Add mechanism to handle circular references when serializing (dotnet/runtime #30820)](https://github.com/dotnet/runtime/issues/30820)
472479

480+
::: moniker range=">= aspnetcore-5.0"
481+
482+
## Blazor JavaScript isolation and object references
483+
484+
Blazor enables JavaScript isolation in standard [JavaScript modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules). JavaScript isolation provides the following benefits:
485+
486+
* Imported JavaScript no longer pollutes the global namespace.
487+
* Consumers of a library and components aren't required to import the related JavaScript.
488+
489+
For example, the following JavaScript module exports a JavaScript function for showing a browser prompt:
490+
491+
```javascript
492+
export function showPrompt(message) {
493+
return prompt(message, 'Type anything here');
494+
}
495+
```
496+
497+
Add the preceding JavaScript module to a .NET library as a static web asset (`wwwroot/exampleJsInterop.js`) and then import the module into the .NET code using the <xref:Microsoft.JSInterop.IJSRuntime> service. The service is injected as `jsRuntime` (not shown) for the following example:
498+
499+
```csharp
500+
var module = await jsRuntime.InvokeAsync<JSObjectReference>(
501+
"import", "./_content/MyComponents/exampleJsInterop.js");
502+
```
503+
504+
The `import` identifier in the preceding example is a special identifier used specifically for importing a JavaScript module. Specify the module using its stable static web asset path: `_content/{LIBRARY NAME}/{PATH UNDER WWWROOT}`. The placeholder `{LIBRARY NAME}` is the library name. The placeholder `{PATH UNDER WWWROOT}` is the path to the script under `wwwroot`.
505+
506+
<xref:Microsoft.JSInterop.IJSRuntime> imports the module as a `JSObjectReference`, which represents a reference to a JavaScript object from .NET code. Use the `JSObjectReference` to invoke exported JavaScript functions from the module:
507+
508+
```csharp
509+
public async ValueTask<string> Prompt(string message)
510+
{
511+
return await module.InvokeAsync<string>("showPrompt", message);
512+
}
513+
```
514+
515+
::: moniker-end
516+
473517
## Additional resources
474518

475519
* <xref:blazor/call-dotnet-from-javascript>

aspnetcore/blazor/components/class-libraries.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ private static string GetLoggingDirectory()
181181

182182
For more information, see [Annotating APIs as unsupported on specific platforms (dotnet/designs GitHub repository](https://github.com/dotnet/designs/blob/main/accepted/2020/platform-exclusion/platform-exclusion.md#build-configuration-for-platforms).
183183

184+
## Blazor JavaScript isolation and object references
185+
186+
Blazor enables JavaScript isolation in standard [JavaScript modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules). JavaScript isolation provides the following benefits:
187+
188+
* Imported JavaScript no longer pollutes the global namespace.
189+
* Consumers of the library and components aren't required to manually import the related JavaScript.
190+
191+
For more information, see <xref:blazor/call-javascript-from-dotnet#blazor-javascript-isolation-and-object-references>.
192+
184193
::: moniker-end
185194

186195
## Build, pack, and ship to NuGet

0 commit comments

Comments
 (0)