The onbeforeunload event fires when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. DotNetBrowser API allows handling this dialog using DialogHandler API. By default, the dialog is displayed. Using your custom implementation of theDialogHandler, you can handle this dialog in your own way. For example, you can display your custom message dialog or suppress the dialog and don't allow unloading web page.


The following sample demonstrates how to handle the onbeforeunload dialog:


MainWindow.xaml

<Window x:Class="WPFDialogHandlerSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpf="clr-namespace:DotNetBrowser.WPF;assembly=DotNetBrowser"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="mainLayout">
        <wpf:WPFBrowserView Name="browserView"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using DotNetBrowser;
using DotNetBrowser.WPF;

namespace WPFDialogHandlerSample
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            // Initialize WPF Application UI.
            InitializeComponent();

            var defaultHandler = browserView.Browser.DialogHandler;
            browserView.Browser.DialogHandler = new CustomDialogHandler(defaultHandler);

            // Load HTML from string into BrowserView.
            browserView.Browser.LoadHTML("<html><body onbeforeunload='return myFunction()'>" +
                "<a href='http://www.google.com'>Click here to leave</a>" +
                "<script>function myFunction() { return 'Leave this web page?'; }" +
                "</script></body></html>");
        }
    }

    public class CustomDialogHandler : DialogHandler
    {
        private DialogHandler defaultHandler;

        public CustomDialogHandler(DialogHandler defaultHandler)
        {
            this.defaultHandler = defaultHandler;
        }

        public CloseStatus OnBeforeUnload(UnloadDialogParams parameters) {
            CloseStatus returnValue = CloseStatus.CANCEL;
            String title = "Confirm Navigation";
            String message = parameters.Message;
            MessageBoxResult result = MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                returnValue = CloseStatus.OK;
            }
            return returnValue;
        }

        public void OnAlert(DialogParams parameters)
        {
            defaultHandler.OnAlert(parameters);
        }

        public CloseStatus OnConfirmation(DialogParams parameters)
        {
            return defaultHandler.OnConfirmation(parameters);
        }

        public CloseStatus OnFileChooser(FileChooserParams parameters)
        {
            return defaultHandler.OnFileChooser(parameters);
        }

        public CloseStatus OnPrompt(PromptDialogParams parameters)
        {
            return defaultHandler.OnPrompt(parameters);
        }

        public CloseStatus OnReloadPostData(ReloadPostDataParams parameters)
        {
            return defaultHandler.OnReloadPostData(parameters);
        }

        public CloseStatus OnSelectCertificate(CertificatesDialogParams parameters)
        {
            return defaultHandler.OnSelectCertificate(parameters);
        }
    }
}