none
Double execution onbeforeunload after trying to close tab RRS feed

  • Question

  • IE 11.0.9600

    I have script:

        public interface ICloseHandler
        {
            string Message { get; set; }
    
            void Initialize();
        }
    
        public class InBrowserCloseHandler : ICloseHandler
        {
            private const string ScriptableObjectName = "InBrowserCloseHandler";
    
            #region ICloseHandler Members
    
            public void Initialize()
            {
                HtmlPage.RegisterScriptableObject(ScriptableObjectName, this);
                string pluginName = HtmlPage.Plugin.Parent.Id;
    
                HtmlPage.Window.Eval(string.Format(
                    @"window.onbeforeunload = function () 
                    {{
                        var slApp = document.getElementById('{0}').getElementsByTagName('object')[0];
                        var result = slApp.Content.{1}.OnBeforeUnload();
                        if(result.length > 0)
                            return result;
                    }}",
                    pluginName, ScriptableObjectName)
                    );
            }
    
            public string Message { get; set; }
    
            public static bool StopActivity { get; set; }
    
            #endregion
    
            [ScriptableMember]
            public string OnBeforeUnload()
            {
                if (!StopActivity)
                {
                    return Message;
                }
                else
                {
                    StopActivity = false;
                    return string.Empty;
                }
            }
        }
    
        public class OutOfBrowserCloseHandler : ICloseHandler
        {
            #region ICloseHandler Members
    
            public void Initialize()
            {
                Application.Current.MainWindow.Closing +=
                    (s, e) =>
                    {
                        if (!StopActivity)
                        {
                            MessageBoxResult boxResult = MessageBox.Show(
                                Message,
                                string.Empty,
                                MessageBoxButton.OKCancel);
                            if (boxResult == MessageBoxResult.Cancel)
                                e.Cancel = true;
                        }
                        else
                        {
                            StopActivity = false;
                        }
                    };
            }
    
            public string Message { get; set; }
    
            public static bool StopActivity { get; set; }
    
            #endregion
        }
    
        public class PowerfullCloseHandler : ICloseHandler
        {
            private readonly ICloseHandler _closeHandler;
    
            public PowerfullCloseHandler()
            {
                if (!Application.Current.IsRunningOutOfBrowser)
                    _closeHandler = new InBrowserCloseHandler();
                else
                    _closeHandler = new OutOfBrowserCloseHandler();
            }
    
            #region ICloseHandler Members
    
            public string Message
            {
                get { return _closeHandler.Message; }
                set { _closeHandler.Message = value; }
            }
    
            public static bool StopActivity
            {
                get
                {
                    if (!Application.Current.IsRunningOutOfBrowser)
                        return InBrowserCloseHandler.StopActivity;
                    else
                        return OutOfBrowserCloseHandler.StopActivity;
                }
                set
                {
                    if (!Application.Current.IsRunningOutOfBrowser)
                        InBrowserCloseHandler.StopActivity = value;
                    else
                        OutOfBrowserCloseHandler.StopActivity = value;
                }
            }
    
            public void Initialize()
            {
                _closeHandler.Initialize();
            }
    
            #endregion
        }

        public partial class App : Application
        {
            private MyProject.Helpers.Addons.ICloseHandler _handler;
    
            public App()
            {
                InitializeComponent();
    
                _handler = new PowerfullCloseHandler();
                _handler.Initialize();
                _handler.Message = Strings.Message_WarningClosingWindow;
            }
    }


    Before downloading files on the web site I doing "StopActivity = true":

    MyProject.Helpers.Addons.PowerfullCloseHandler.StopActivity = true;
    HtmlPage.Window.Navigate(new Uri(String.Format(BLL.MyProjectAppHost + "/download.aspx?file={0}&filename={1}", new string[] { e.OutGUID, e.OutFileName })));

    that would not receive a warning.

    But when I try to close a tab, and I cancel the action, the next time I download the file I get an event "onbeforeunload" twice:

                    @"window.onbeforeunload = function () 
                    {{
                        var slApp = document.getElementById('{0}').getElementsByTagName('object')[0];
                        var result = slApp.Content.{1}.OnBeforeUnload();
                        if(result.length > 0)
                            return result;
                    }}",

    As a result during the download begin to receive warning.

    Google Chrome does not have such a problem. Google Chrome works great.


    Wednesday, April 23, 2014 12:29 PM

Answers

All replies