Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
$(document).ready(function(){
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? '' : 'beforeunload'; /// make IE7, IE8 compitable

myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = $('#myModal').modal('show'); // a space
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
});

What I have tried:

i want to first confirmation, and when i click stay on page then will come model but it not working properly in mozilla what happen both are come same time model and confirmation in mozilla and it working in chrome plz help me..
Posted
Updated 27-Jun-16 8:42am
v3
Comments
Kornfeld Eliyahu Peter 27-Jun-16 9:44am    
Are you sure about your code sample...Check again the source you copied from...
Your problem is not clear to me...Do you see the confirmation message (any) in Firefox?
jp gangwar 27-Jun-16 10:03am    
confirmation message come but problem is that window model and default confirmation messsage both come same time if i closed tab

1 solution

The "beforeunload" event is very restrictive. In IE / Edge, all you can do is display a custom message by returning it from the event handler. In Firefox and Chrome, you can't even do that - any custom message you return will be ignored, and the browser will display its standard "data may be lost" message instead.

You certainly can't open windows or display modal dialogs. If you could, every malicious site on the Internet would do it to prevent you from leaving the page.

Stick with returning a simple string from your event handler. Browsers which allow it will display your message, and those that don't will display a standard message instead.
 
Share this answer
 
Comments
jp gangwar 28-Jun-16 6:30am    
so what can do ?
Richard Deeming 28-Jun-16 7:24am    
As I said, all you can do with that event is return a string with the message you want to display. Browsers that support it will display that message, and browsers that don't will display a standard message instead.

If you need to do anything more complicated, then you'll need to intercept and cancel any event that would result in navigating away from the page - clicking on links, submitting forms, etc. It's a lot of work, and it still won't be foolproof.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900