0

I need to trigger some method before living my site. I'm using

$(window).blur(blurFunc); 

successfully for all browsers except Opera, which is not triggering this event.

I did some research finding

$(window).bind('beforeunload',blurFunc); 

might work and indeed this worked for me in one site. However, when implementing this in another site this event was not triggered (without any differences between the sites I can think of (except jquery version which I checked and not effecting).

How can I trigger a function before living page in Opera? - either beforeunload or blur or any other function will be fine...

Opera version is 12.16

Ary
  • 1
  • 1
  • For opera, you could try using onunload event instead, that's just a guess and anyway i have no idea what your are trying to do here, blurFunc??? – A. Wolff Mar 27 '14 at 12:20
  • @A.Wolff - `onunload` or `$(window).bind('onunload');` does not work. It is also not something to do with what `blurFunc` is doing - changing it to do `a=1+1;` and still not working. – Ary Mar 27 '14 at 13:08
  • Should be `$(window).bind('unload');` instead but really i have no idea how opera handle these kind of events, i mean behaviour for these kind of events. EDIT: i see in your comment that you are sending an ajax request, then forget it, there is no for sure any kind of workaround working across browsers. FF could handle it by setting request synchronously but, e.g, chrome won't and Opera, well, i don't know... – A. Wolff Mar 27 '14 at 13:09

1 Answers1

0

You don't want the blur event. It may or may not fire, and may fire at times you don't want.

With beforeunload, it depends a lot on what's happening in blurFunc, which you haven't told us. The things you can do in a beforeunload handler are quite constrained. One of the big things people trip over is that if you try to do an ajax call, if it's an asynchronous one (the default), it won't work. I wouldn't recommend relying on a synchronous one either, frankly, although I've heard that they work well enough.

beforeunload is a very unusual event. If you want to prompt the user with a message as they leave the page, I've never done that with jQuery, only by assigning a handler directly:

window.onbeforeunload = function() {
    var msg = "Really leave?";
    if (window.event) {
        window.event.returnValue = msg;
    }
    return msg;
};

Note that we don't actually show the message, we just hand it to the browser as the return value of the function (and as the returnValue property of the event, if we're on IE). That tells the browser to show the message with the usual "stay" or "leave" options.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks @T.J.Crowder for the answer. I'm indeed using `blurFunc` for ajaxing, however I'm certain that the event is not triggered because of something eles (double checked and switch the function to `alert(1);` which is still not working). Also tried `window.onbeforeunload` instead of `beforeunload` without any change of behavior... – Ary Mar 27 '14 at 12:57
  • @Ary: As I said, `beforeunload` is a very unusual event, and the things you're allowed to do is quite restricted. I wouldn't be surprised to find that Opera won't let you do `alert`, for instance. – T.J. Crowder Mar 27 '14 at 12:59
  • @Ary: Ah, looks like Opera 12 doesn't support `onbeforeunload` *at all*: http://stackoverflow.com/questions/15986731/is-there-any-way-to-make-onbeforeunload-work-in-opera Opera 15+ support it, but not the earlier ones. – T.J. Crowder Mar 27 '14 at 13:00
  • nothing works for this event, even just `a=1+1;` (the debugger just don't stop in this method...) – Ary Mar 27 '14 at 13:10
  • @Ary: Did you see my comment above about Opera 12? – T.J. Crowder Mar 27 '14 at 13:11
  • @T.C.Crowder - yes, and it works with `$(window).blur(blurFunc);`... thanks a lot! – Ary Mar 27 '14 at 13:31