skip to Main Content

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


    1. you need to excute that method(window.exitTabFunctions.setupExitTabDetection). So you need to excute JS by IJSRuntime.InvokeVoidAsync().
    2. That code works in only Firefox and IE. So you need to add return "Sure?" below the event.returnValue = "Sure?";.
    Login or Signup to reply.
  1. You can set internal or external navigation using NavigationLock component which was introduced in .net 7.

    <NavigationLock ConfirmExternalNavigation="true" />
    

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.routing.navigationlock?view=aspnetcore-7.0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search