Close a browser window from a page in Blazor WebAssembly using JavaScript Interop with the window.close() method. The window.close() method closes the currently opened window.
In the following example, open a new browser window and close it using the window.close() method with a button onclick event.
[Index.razor]
@page "/"
@inject IJSRuntime JsRuntime
<button @onclick="@(e => OnButtonClick("open"))">Open Window</button>
<button @onclick="@(e => OnButtonClick("close"))">Close Window</button>
@code {
private void OnButtonClick(string value)
{
JsRuntime.InvokeVoidAsync($"window.{value}");
}
}
Share with