I want to show confirm alert when user tries to leave page or tab. How to do it in blazor c#?
I tried to add javascipt script with "beforeunreload" event, but I don’t know how to do it
window.exitTabFunctions = {
setupExitTabDetection: function () {
window.addEventListener("beforeunreload", function (event) {
event.preventDefault();
event.returnValue = "Sure?";
});
}
};
2
Answers
window.exitTabFunctions.setupExitTabDetection
). So you need to excute JS byIJSRuntime.InvokeVoidAsync()
.return "Sure?"
below theevent.returnValue = "Sure?";
.You can set internal or external navigation using NavigationLock component which was introduced in .net 7.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.routing.navigationlock?view=aspnetcore-7.0