Anand Neema
Anand Neema

Reputation: 660

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 ??

Upvotes: 1

Views: 1159

Answers (2)

bhaveshv05
bhaveshv05

Reputation: 141

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?"); 
 });
});

Upvotes: 1

gantoine
gantoine

Reputation: 1275

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?");
});

Upvotes: 0

Related Questions