简体   繁体   中英

Confirmation Before Closing of Tab/Browser using JavaScript

How to ask confirmation from user before he leaves the page?

I searched for this question in various places, but all that they mention is the use of javascript window.unload & window.onbeforeunload.

What i want. I want a message box to appear and to ask me if I really want to close the page or not, with two buttons and if I click 'NO' then this tab won't be closed and if 'YES' then submit the form then closed tab.

   window.onbeforeunload = function (e) {
        e = e || window.event;

        if (e) {
            e.returnValue = 'Sure?';
            $("#myForm").submit();
        }       
        return 'Sure?';
    };

For that scenario, you have to make some activity on the page. Some click or some changes on the page.

Without any activity on the current page, the tab will not show the message as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM