简体   繁体   中英

Browser tab close event in android browser using javascript

Is there any way onbeforeunload event works, when we close any tab in android browser.

It is working fine in desktop browsers.

Basically in android browser i need "Leave this page" or "stay on this page" pop up in all devices.

Or Is it possible or not ??

I had same issue and found solution, Below code is working for me, I thought I should share solution which works for me. I hope it would help other.

$(document).ready(function()
 {  $(window).bind("beforeunload", function() { 
    return confirm("Do you really want to close?"); 
 });
});

Some browsers don't support onbeforeunload , instead opting for onunload . This should work on desktop and mobile:

window.onunload = window.onbeforeunload = function(e) {
    return confirm("Are you sure you want to leave this page?");
});

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