Alan Shortis
Alan Shortis

Reputation: 1109

AJAX calls aborted in IE8 and below when called using window.onunload

I have a jQuery AJAX call which is made every 5 minutes and when the user leaves the page (using different parameters).

/* Remove locks when leaving page - AJAX 'aborted' by IE8 and below */
    window.onunload = function () {
        manageLocks(2)
    }

    /* Refresh locks every 5 minutes - AJAX works fine in all browsers */
    window.setInterval(function () {manageLocks(1);}, 30000);

    function manageLocks(mode) {
        $.ajax({
            type: "POST",
            url: "/System/ManageLocks",
            data: '{ "action" : ' + mode + '}',
            contentType: "application/json",
            dataType: "json",
            cache: false
        });
    }

I do not think this is an issue with the AJAX call, rather how that function is called. It works fine in all browsers when called using window.setInterval but is shown as 'Aborted' in the network panel in IE8 and below when called using window.onunload.

Is anyone aware of old IE versions blocking certain functions when called this way, or shaky support of onunload? Is there another way I can get this to work in older IE versions?

As long as I can get the managelocks function to be called every 5 minutes AND when the user leaves the page (I am aware that as it is, the function isn't called when the browser is closed - I can live with that) I'm not too fussy how it happens.

Thanks.

Upvotes: 0

Views: 654

Answers (1)

Roy James Schumacher
Roy James Schumacher

Reputation: 646

try setting the async param of your ajax call to false so it executes the call then unloads the page. at the minute it is trying to execute the ajax call at the same time as the page being unloaded and it seems the page is unloaded before the calls completed so its cancelling the ajax call so by setting async to false will allow the ajax call to finish before the page is unloaded

Upvotes: 2

Related Questions