Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have to save data while closing the browser. I have done it on page
"onbeforeunload"
event but it is working only in IE. can you help me in saving data using other browser
Posted

Well, I am not sure what you are exactly doing to save the data. But to make the event beforeunload to work for other browsers, have a look at this link[^]. Though, it talks for FF, but works same for others browsers as well.

Though, it is off topic but lately I have been loving jQuery [^]for its capability to handle these cross broswer implementation. With jQuery you can do something like this.

$(document).ready(function ()
{
    jQuery(window).bind('beforeunload', function ()
    {
        //save my data
        SaveData();
    });
});


EDIT: adding more explanation to OPs question to where to write this code?

add this code in the head section. also look at https://developer.mozilla.org/en/DOM/window.onbeforeunload
XML
<head>
<script>
window.onbeforeunload = function (e)
{
  var e = e || window.event;
  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }
  // For Safari
  return 'Any string';
};
</script>
</head>
 
Share this answer
 
v3
Comments
Rachit Barjatya 21-Feb-11 6:17am    
Where i have to write this code?
Rachit Barjatya 22-Feb-11 2:33am    
Hello - Manas. you have given me the java script but it is not working. so please can tell me where i have to write the jquery u gave me...
Hi Rachit,

OnBeforeUnload appears to be one of those Microsoft IE quirks they have developed. It only works in IE and Safari so far as I can tell. However, the standard event is OnUnload. This owkrs in the 3 other mian browsers; Firefox, Opera and Google Chrome. The code I managed to get working in all browsers was:
HTML
<html>
<head>
<title></title>
<script type="text/javascript">
    function Closing()
    {
        alert('Closing');
    }
</script>
</head>

<body onunload="Closing();" onbeforeunload="Closing();">
</body>
</html>


So simply replace the alert in the closing method with your save data code.

Hope this helps,

Ed :)
 
Share this answer
 
Comments
Rachit Barjatya 21-Feb-11 6:40am    
"onunload" works fine with with IE and Chrome but not working in FF.
Ed Nutting 21-Feb-11 6:46am    
Aah..sorry just retried FF with only OnUnload event. Turns out FF accepts the OnBeforeUnload event when set in the body tag. If you can't get that to work then I would suggest it is a version issue. I have FF 5.0 (2009 is the copyright date in the Help About box). If you have a different version, that may be the issue.

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