BlazorJS 2.1.3
See the version list below for details.
dotnet add package BlazorJS --version 2.1.3
NuGet\Install-Package BlazorJS -Version 2.1.3
<PackageReference Include="BlazorJS" Version="2.1.3" />
paket add BlazorJS --version 2.1.3
#r "nuget: BlazorJS, 2.1.3"
// Install BlazorJS as a Cake Addin #addin nuget:?package=BlazorJS&version=2.1.3 // Install BlazorJS as a Cake Tool #tool nuget:?package=BlazorJS&version=2.1.3
BlazorJS
BlazorJS is a small package to have better interaction with JavaScript.
- Use a Scripts Component on every page or component to load JavaScript or Stylesheet files when not loaded, and unload automatically on dispose if you want to.
- This library provides extensions for IJSRuntime for dynamic invocation that eliminates the requirement to create JS wrapping functions for everything
- A possibility to hook events on a Blazor component or page.
- Import a module and create a JS object reference from it
Using Scripts Component
Add the nuget Package BlazorJS
to your blazor project
Open _Imports.razor
from your project and add using for BlazorJS
...
@using BlazorJS
Now your are able to include every javascipt file easily to your pages or components.
For example open any page like the index.razor
and add
<Scripts src="js/myjsfile.js"></Scripts>
This component can also load stylesheet files
<Scripts src="js/myjsfile.js,css/mystyle.css"></Scripts>
Include multiple javascript files
Multiple js files can be loaded with a comma seperator ,
<Scripts src="js/myjsfile.js, js/myjsfile2.js"></Scripts>
Extension Dynamic JS Invocation
The Dynamic Invocation extension for IJSRuntime allows for dynamic invocation of JavaScript functions from C#. This extension provides a single method, DInvokeVoidAsync, which takes in two arguments: a function to be invoked and an array of objects to be passed as arguments to that function.
Usage
The DInvokeVoidAsync
method can be called on any instance of IJSRuntime, and takes in general two arguments: a function to be invoked and an array of objects to be passed as arguments to that function.
Here are some examples of how the method can be used:
Simple Call
await jsRuntime.DInvokeVoidAsync(window => window.alert("test"));
Passing Parameters
if in your scope a variable is available. (In the base Blazor Sample the currentCount for example) Something like this is not enough
// DONT COPY THIS!! SAMPLE FOR NOT WORKING
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount));
For doing this we need to pass the parameters. You can do it like this
await jsRuntime.DInvokeVoidAsync((window, c) => window.alert(c), currentCount);
await jsRuntime.DInvokeVoidAsync((window, c, p2, p3..) => window.alert(c), currentCount, param2, param3, ...);
await jsRuntime.DInvokeVoidAsync((window, c, x) =>
{
window.alert(c);
window.console.log(x);
}, currentCount, "Flo");
or you can add parameters with the class JSArgument and an approach like this
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount), JSArgument.For(currentCount).ToArray());
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount + " - " + date + name), JSArgument.For(currentCount).And(date).And(name));
..if you here need more parameters you can simple add them to an array or use the And method
var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
var name = "John";
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount + " - " + date + name), JSArgument.For(currentCount).And(date).And(name));
Using return values
All seen samples are also callable with an generic arg to use return values.
You can use return values from javascript functions like this
var res = await jsRuntime.DInvokeAsync<string>(window => window.prompt());
Console.WriteLine(res);
Also you can reuse the results as parameter and writing something like this
var hash = await jsRuntime.DInvokeAsync<string>(window =>
{
window.alert(currentCount);
return window.location.hash + "_" + currentCount;
}, new[] { JSArgument.For(currentCount) });
await jsRuntime.DInvokeVoidAsync(document => document.location.hash = hash, new[] { JSArgument.For(hash) });
// After alerting the currentCount we update the url in browser like this #1_2_3_4...
It is important to note that the function passed to DInvokeVoidAsync must be a lambda expression that takes in a dynamic and dynamic parameter.
Conclusion This extension provides a simple and easy way to invoke JavaScript functions from C# with support for dynamic arguments.
Simple event interop helper
A simple possibility to hook events that should be extended more but currently supports an easy OnBlur extension for any element.
private BlazorJSEventInterop<PointerEventArgs> _jsEvent;
_jsEvent = new BlazorJSEventInterop<PointerEventArgs>(_jsRuntime);
await _jsEvent.OnBlur(OnFocusLeft, ".element-selector");
private Task OnFocusLeft(PointerEventArgs arg)
{
return Task.CompletedTask;
}
You can also use it manually with any event you want to.
private BlazorJSEventInterop<PointerEventArgs> _jsEvent;
_jsEvent = new BlazorJSEventInterop<PointerEventArgs>(_jsRuntime);
await _jsEvent.AddEventListener("NameOfEvent", async args => { await YourCallBack(); }, ".element-selector");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.Components (>= 3.1.0)
- Microsoft.AspNetCore.Components.Web (>= 3.1.0)
- Microsoft.Extensions.FileProviders.Embedded (>= 3.1.1)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.13)
- Microsoft.Extensions.FileProviders.Embedded (>= 6.0.13)
- Microsoft.JSInterop (>= 6.0.13)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.2)
- Microsoft.Extensions.FileProviders.Embedded (>= 7.0.2)
- Microsoft.JSInterop (>= 7.0.2)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on BlazorJS:
Package | Downloads |
---|---|
MudBlazor.Extensions
MudBlazor.Extensions is a small extension library for MudBlazor from https://mudblazor.com/ |
|
AuralizeBlazor
AuralizeBlazor is a wrapper component for audioMotion-analyzer. |
|
MudExRichTextEditor
MudExRichTextEditor is a custom reusable control that allows us to easily consume Quill combining in a MudBlazor project. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on BlazorJS:
Repository | Stars |
---|---|
fgilde/MudBlazor.Extensions
MudBlazor.Extensions from https://www.mudex.org is a small extension for MudBlazor from https://mudblazor.com
|
Version | Downloads | Last updated |
---|---|---|
2.1.6 | 16,185 | 6/28/2024 |
2.1.5 | 30,583 | 2/2/2024 |
2.1.4 | 10,963 | 11/25/2023 |
2.1.3 | 138 | 11/24/2023 |
2.1.2 | 5,538 | 11/2/2023 |
2.0.9 | 7,406 | 9/13/2023 |
2.0.8 | 3,434 | 7/29/2023 |
2.0.7 | 219 | 7/28/2023 |
2.0.6 | 1,056 | 7/14/2023 |
2.0.5 | 366 | 6/13/2023 |
2.0.4 | 9,304 | 4/21/2023 |
2.0.3 | 5,181 | 3/2/2023 |
2.0.2 | 1,403 | 2/15/2023 |
2.0.1 | 311 | 2/8/2023 |
2.0.0 | 2,782 | 1/23/2023 |
1.0.4 | 2,383 | 12/9/2022 |
1.0.3 | 329 | 12/9/2022 |
1.0.2 | 327 | 12/9/2022 |
1.0.1 | 364 | 11/19/2022 |
1.0.0 | 595 | 2/3/2020 |
0.1.0-preview.200127215053 | 306 | 1/28/2020 |