Blazor.JSInterop 1.0.4

Suggested Alternatives

Persilsoft.Blazor.JSInterop

Additional Details

Now, you can use Persilsoft.Blazor.JSInterop

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Blazor.JSInterop --version 1.0.4                
NuGet\Install-Package Blazor.JSInterop -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blazor.JSInterop" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.JSInterop --version 1.0.4                
#r "nuget: Blazor.JSInterop, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Blazor.JSInterop as a Cake Addin
#addin nuget:?package=Blazor.JSInterop&version=1.0.4

// Install Blazor.JSInterop as a Cake Tool
#tool nuget:?package=Blazor.JSInterop&version=1.0.4                

Blazor.JSInterop

A library containing abstract classes for loading JavaScript and WebAssembly modules


Example for JavaScript module

We have the following JavaScript module named geolocationModule.js with a function that gets the user's coordinates

const getPosition = () => {
    return new Promise((resolve, reject) => {
        if ("geolocation" in navigator) {
            navigator.geolocation.getCurrentPosition(returnPosition, returnError);
        }

        function returnPosition(position) {
            resolve({
                latitude: position.coords.latitude,
                longitude: position.coords.longitude
            });
        }

        function returnError(error) {
            let errorMessage;
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    errorMessage = "User denied the request for Geolocation.";
                    break;
                case error.POSITION_UNAVAILABLE:
                    errorMessage = "Location information is unavailable.";
                    break;
                case error.TIMEOUT:
                    errorMessage = "The request to get user location time out.";
                    break;
                case error.UNKNOWN_ERROR:
                    errorMessage = "An unknown error ocurred.";
                    break;
                default:
                    errorMessage = "An error has ocurred.";
            }

            reject(errorMessage);
        }
    });
}

export { getPosition };

Now, you need to create a service to interoperate with the JavaScript code. Here the Blazor.JSInterop library makes things easier for us

using Microsoft.JSInterop;

public record struct GeolocationLatLong(double Latitude, double Longitude);

public class GeolocationService(IJSRuntime jsRuntime)
    : JSLoaderServiceBase(jsRuntime, $"./js/geolocationModule.js")
{
    public async ValueTask<GeolocationLatLong> GetPosition() =>
        await InvokeAsync<GeolocationLatLong>("getPosition");
}

Register the service.

builder.Services.AddScoped<GeolocationService>();

Finally you can use it.

@page "/get-position"
@inject GeolocationService Geolocation

<button class="btn btn-primary mb-2" @onclick=ShowPosition>Show my position</button>
<textarea class="form-control" rows="3" disabled @bind=Result>
</textarea>

@code {
    private string Result;

    private async Task ShowPosition()
    {
        var Coords = await Geolocation.GetPosition();
        Result = $"Latitude: {Coords.Latitude}, Longitude: {Coords.Longitude}";
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated