2

I need to execute some code before leaving the browser, I implemented this:

    window.onbeforeunload = function () { return 'Are you sure?'; };
    window.onunload = function () { releaseLocking(); };

It works pretty well with Google Chrome. When closing GC: a message is shown,

  • if I click on the button 'stay on the page', nothing will happen. Perfect.
  • if I click on the button 'leave the page', the code under releaseLocking will be executed. Perfect.

I have problems with Internet Explorer.

  • if I click on the button 'stay on the page', nothing will happen. Perfect.
  • if I click on the button 'leave the page', the code under releaseLocking won't get executed.

Any idea?

I searched a lot but didnt found a solution.

Thanks.


UPDATE

var releaseLocking = function() {
    // Release only if lock is set on the current user
    if (transport().lockedById() == 5) { // 5 is dummy (Jean Dupont for testing)
        transport().lockedById(null);
        transport().lockedTime(null);
        return ctxTransport.saveChanges(SILENTSAVE);
    }
};
ComFreek
  • 29,044
  • 18
  • 104
  • 156
Bronzato
  • 9,438
  • 29
  • 120
  • 212

2 Answers2

0

It's seems IE have bug where the unload event wouldn't fire on a specific page on a site.

the unload event never fired since all the contents of the page hadn't finished loading before it's navigated to another page.

try stopping it before you unload

try this:

function fixUnload() {
    // Is there things still loading, then fake the unload event
    if (document.readyState == 'interactive') {
        function stop() {
            // Prevent memory leak
            document.detachEvent('onstop', stop);

            // Call unload handler
            unload();
        };

        // Fire unload when the currently loading page is stopped
        document.attachEvent('onstop', stop);

        // Remove onstop listener after a while to prevent the unload function
        // to execute if the user presses cancel in an onbeforeunload
        // confirm dialog and then presses the stop button in the browser
        window.setTimeout(function() {
            document.detachEvent('onstop', stop);
        }, 0);
    }
};

function unload() {
    alert('Unload event occured.');
};

window.attachEvent('onunload', unload);
window.attachEvent('onbeforeunload', fixUnload);
Ofir Hadad
  • 1,800
  • 3
  • 26
  • 47
0

These types of events are very unreliable.. what if the user's browser crashes (lol ie!) I was able to achieve the same outcome by using a heartbeat and polling the server every 20-30 seconds (or longer idk what your duration may be) and using that to handle the exiting event.. I had a locking mechanism in this app too.

Good luck.

Zach Leighton
  • 1,939
  • 14
  • 24
  • 1
    Do you mean you querry the server every 20-30 seconds to say 'hello, I'm still there, don't release the lock' ? – Bronzato Jun 09 '13 at 19:33
  • Exactly, you can extend it maybe to check for mouse or keyboard events to do a real idle timeout if you wish. Take a look at facebook's xhrs. – Zach Leighton Jun 09 '13 at 19:34