views:

30

answers:

1

I have a html page that displays some basic account information and begins a long-ish running jQuery AJAX request to retrieve more detailed data. While the Ajax request is in progress it is possible for the user to click a button that has an onclick event to navigate to a new page using location.assign.

Unfortunately if the button is clicked before the ajax request is complete nothing will happen until the ajax request completes. This is a live server issue. I want the user to be able to immediately navigate away. FF and Chrome appear to behave better but since this is a corporate intranet application they are not really an option.

The following code is similar to the page in question:

<html>
    <head>
        <script src="/js/jquery-1.3.2.min.js" type="text/javascript"> </script>

        <script type="text/javascript">
<!--
    $(function () {
        jQuery.ajax({
            type: 'GET',
            url: '/long-running-partial-html-ajax-endpoint',
            success: function (result) {
                $('#detail').html(result); });
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                $('#detail').html('Failed to load additional information:<br />' + textStatus + '<br />' + errorThrown);
            }
        });
    });
//-->
        </script>
    </head>
    <body>
        <h2>Account Information</h2>
        <div>Some basic details here</div>
        <div><button onclick="location.assign(&quot;/somewhere-else&quot;)" type="button">Go somewhere else now</button></div>
        <div id="detail">
            <img src="/ajax-loading-animation.gif" alt="Loading ..." />
            Loading ...
        </div>
    </body>
</html>

Things I've tried in the debugger (not on live) already:

  • using a plain anchor rather than a scripted button
  • using xhr.abort() before the location.assign
  • put alerts around the location.assign to reassure myself that the code is executing when expected

Observation:

  • IE stops animating the gif as soon as the button is clicked.
  • FF/Chrome must automatically abort the ajax request as the jQuery ajax error event is fired

Has anyone come across this issue before? Have you a resolution that will make the navigation more responsive?

A: 

Have you tried to call the ajax method after the page is loaded

  <body onload="myFunctionThatCallsAjax()">

There are some browser behavior differences when you embed Javascript in the HTML code. Using onload will ensure this is not an issue.

ring0
Thanks for your interest ring0. I tried this out but it made no difference. I am using the jQuery document ready function (`$(function() {});`) to start the ajax request anyway which I believe would achieve the same effect.
WooWaaBob