Of all the browsers, it seems that only Opera doesn't support onunload/onbeforeunload events. (It's been fifteen years now, Opera!) Solutions for this issue have been covered many times, here for example: onbeforeunload support detection
Unfortunately, as of Opera 11.51, ("onbeforeunload" in window) == true
, but the actual onbeforeunload event is never executed!
My web application needs to send data to server when a user leaves the page; I'm using a synchronous ajax request for this. It looks like I have to resort to using a "Save" button somewhere on the page to cover up for Opera issues. However, I don't want this button to confuse users whose browsers are capable of auto-saving through ajax, so I'd really like the button to only show up in Opera.
Is my only choice browser-detection? The problem is, Opera has an option to disguise itself as other browsers anyway.
onuload
for Opera ?body.onunload
works in Opera 11.5, unless you're callingwindow.location.reload()
<body onunload = 'alert("test")'>
,body.onunload = function() {alert("test")};
andbody.unload(function() {alert("test")});
all don't work on my machine. Opera 11.51, no add-ons, Windows 7 64bit.<body onunload="alert('!')"><p onclick="window.location.reload();">Click here to reload</p><p><a href="http://ya.ru">Get away</a></p></body>
- I have onunload working in this case when you leave the page. Anyway, you won't get the code executed in Opera if you close the page/app, so a better way may be to implement some other kind of data saving. How about some local storage?