0

I have built a simple web application using ASP.net where I close a web browser tab, and it will give the user a warning message via a pop-up box that he is about to close the browser.

The following javascript code I used to add the pop-up box feature:

window.onbeforeunload = function () {
   return "Are you sure";
};

This works on Internet Explorer but not on chrome. I haven't checked Firefox.

I know chrome you can't have custom messages, but I can't get the pop-up box working at all.

Is there any alternative in creating a pop-up box warning that works on all main browser like internet explorer, chrome and Firefox?

Also, I have tested this chrome latest version and the version 66.

Edit

This is not a duplicate of window.onbeforeunload in Chrome: what is the most recent fix?

The user there can display a simple pop up box on chrome. I couldn't display anything. However I noticed something weird when I am in the developer tool window(F12) and debugging the code, I get the pop-up box to appear.

Code Added

I used a default template from ASP.NET Web Forms which creates a simple application.

I have added javascript called CheckBrowserClose.js which contains one line:

window.onbeforeunload = function () { return 'Are you sure?' };

I have added this script to the script bundle.

 public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkID=303951
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
                            "~/Scripts/WebForms/WebForms.js",
                            "~/Scripts/WebForms/WebUIValidation.js",
                            "~/Scripts/WebForms/MenuStandards.js",
                            "~/Scripts/WebForms/Focus.js",
                            "~/Scripts/WebForms/GridView.js",
                            "~/Scripts/WebForms/DetailsView.js",
                            "~/Scripts/WebForms/TreeView.js",
                            "~/Scripts/WebForms/WebParts.js",
                            "~/Scripts/WebForms/CheckBrowserClose.js"));

            // Order is very important for these files to work, they have explicit dependencies
            bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));

            // Use the Development version of Modernizr to develop with and learn from. Then, when you’re
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                            "~/Scripts/modernizr-*"));

            ScriptManager.ScriptResourceMapping.AddDefinition(
                "respond",
                new ScriptResourceDefinition
                {
                    Path = "~/Scripts/respond.min.js",
                    DebugPath = "~/Scripts/respond.js",
                });
        }
    }

0 Answers0