最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detecting browsers that don't support onunloadonbeforeunload - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Sep 23, 2011 at 11:01 Unknown artistUnknown artist 9479 silver badges15 bronze badges 6
  • why not use onuload for Opera ? – c69 Commented Sep 23, 2011 at 11:04
  • body.onunload works in Opera 11.5, unless you're calling window.location.reload() – Lyth Commented Sep 23, 2011 at 11:04
  • <body onunload = 'alert("test")'>, body.onunload = function() {alert("test")}; and body.unload(function() {alert("test")}); all don't work on my machine. Opera 11.51, no add-ons, Windows 7 64bit. – Unknown artist Commented Sep 23, 2011 at 11:09
  • 1 Are you refreshing/reloading the page or navigating to another? <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? – Lyth Commented Sep 23, 2011 at 14:20
  • Yeah, you're absolutely right, my problem is that I need it to save data in all cases and I was testing it by closing the tab mostly. Local storage is a good idea, but as I understand, the changes made in Opera won't be visible if the user logs in from another browser. Also, are we all Russki's here, or is it a time zone thing and Americans are sleeping in? :D – Unknown artist Commented Sep 23, 2011 at 14:58
 |  Show 1 more ment

4 Answers 4

Reset to default 3

I can't reproduce your finding that 'onbeforeunload' in window is true in Opera 11.5x. This is the best way to do it and should still work. Are you sure you haven't left in some definition somewhere, e.g. you've written

onbeforeunload = function (){ ... }

later in the same script that does the feature detection? If you do alert(window.onbeforeunload), what do you see? Could you share a link to the page with the problem?

Opera screwed the pooch on this one. I detect for Opera by looking for window.opera and if it exists, I deny Opera what it can't handle.

Using unload is no good I think, because it occurs too late in the game. Sometimes onbeforeunload is the only thing that'll do the trick. Once again, I just look for opera on the window object, and, if it exists, deny it the things it can't do. :)

PPK talks about it here: http://www.quirksmode/js/detect.html

For anyone stumbling across this post, this is a code snippet I use for detecting onbeforeunload support and if the browser doesn't support it I switch to onunload (note the use of jquery, obviously not required). In my case I use this code (and a little extra) to check if any AJAX requests are still active and stop the user navigating away. Keep in mind that using onunload isn't ideal because the browser will still navigate away from the page but at least it gives you a chance to warn the user that data might have been lost and they should go back and check.

You'll notice I'm using the isEventSupported() function available at https://github./kangax/iseventsupported for cross browser support detecting available events.

// If the browser supports 'onbeforeunload'
if (isEventSupported('beforeunload', window)) {
  $(window).on('beforeunload', function(){
    return 'This page is still sending or receiving data from our server, if you recently submitted data on this page please wait a little longer before leaving.';
  });
} 
// The browser doesn't support 'onbeforeunload' (Such as Opera), do the next best thing 'onunload'.
else {
  $(window).on('unload', function(){
    alert('This page was still sending or receiving data from our server when you navigated away from it, we would remend navigating back to the page and ensure your data was submitted.');
  });
}

See my answer to a similar / duplicated question. Basically, it sets up detection on the very first page on your domain and stores that detection result for all subsequent pages in localStorage. Including working example code.

发布评论

评论列表(0)

  1. 暂无评论