I want to implement a close button to close the browser in the Blazer server-side app.
So I created a close button and called window.close() with javascript.
_host.cshtml
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss"></a>
</div>
<script src="_framework/blazor.server.js"></script>
<script>
function winclose() {
window.close();
}
</script>
</body>
</html>
I need to make it possible to exit by typing in the password. (i'm using Matblazor nuget package)
MainLayout(razor)
@inherits LayoutComponentBase
@inject IJSRuntime JSRuntime
@using System.ComponentModel.DataAnnotations
<div class="sidebar">
<NavMenu />
</div>
<MatDialog @bind-IsOpen="@DialogIsOpen">
<EditForm EditContext="EditContext" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<p>
<MatTextField Style="margin:2px;" Label="Password" @bind- Value="passWordModel.PassWord" Type="password" />
<ValidationMessage For="@(() => passWordModel.PassWord)" />
</p>
<MatButton Class="cancelbtn" Type="reset" @onclick="@CancelClicked">Cancel</MatButton>
<MatButton Type="submit">OK</MatButton>
</EditForm>
</MatDialog>
<div class="main">
<div class="top-row px-4">
<a href="https://learn.microsoft.com/aspnet/" target="_blank">About</a>
<button type="button" class="btn btn-success" @onclick="@ExitClicked">Close</button>
</div>
<div class="content px-4">
@Body
</div>
</div>
MainLayout(code)
@code{
EditContext EditContext { get; set; }
PassWordModel passWordModel = new PassWordModel();
public bool DialogIsOpen { get; set; } = false;
public class PassWordModel
{
[CustomValidation(typeof(PassWordModel), nameof(ValidatePassWord))]
public string PassWord { get; set; }
public static ValidationResult ValidatePassWord(string password, ValidationContext vc)
{
return string.Equals("1234", password)
? ValidationResult.Success
: new ValidationResult("The password is not correct.", new[] { vc.MemberName
});
}
}
protected override void OnInitialized()
{
EditContext = new EditContext(passWordModel);
base.OnInitialized();
}
void ExitClicked()
{
DialogIsOpen = true;
}
void CancelClicked()
{
passWordModel.PassWord = null;
EditContext = new EditContext(passWordModel);
DialogIsOpen = false;
}
private async void HandleValidSubmit()
{
System.Diagnostics.Debug.WriteLine("HandleValidSubmit!");
try
{
await JSRuntime.InvokeVoidAsync("winclose");
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
}
I called window.close() but it doesn't work.
When I press f12 to check the developer tools, the following warning message appears.
'Scripts may close only the windows that were opened by them.'
As a result of googling, I came to the conclusion that I cannot close self window due to security issues.
If so, is there really no solution? Is it impossible at all?