SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast sffc's Avatar
    Join Date
    Jul 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Showing Loading Div for SJAX Requests

    In my current project, I am using SJAX (i.e. synchronous AJAX) requests instead of pure AJAX requests in several places for better usability. One place is on the "unload" event, where the XMLHttpRequest must be synchronous in order for the program to work.

    Anyway, when there is latency on the server, especially in peak traffic hours, it can be confusing to the user to see a frozen page for a couple of seconds while the SJAX request loads. As such, it would be beneficial to have a "loading" div reveal itself while the loading is taking place.

    Here is the concept I currently have in place:
    Code JavaScript:
    document.getElementById("sjaxsubmitting").style.display = "block";
     
    // Start Request
     
    reqobj.open("POST", "myfile.php", false);
    reqobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    if(navigator.userAgent.indexOf("Firefox/3")!=-1){
    	reqobj.sendAsBinary(data);
    }else{
    	reqobj.send(data);
    }
     
    // Finish Request
     
    document.getElementById("sjaxsubmitting").style.display = "none";
    This works as expected in Firefox and Opera, although Opera is a little sluggish at first. However, Safari and Chrome continue to show the frozen screen without displaying the loading div, despite this code. I have not yet tested IE. Oddly enough, when I put a quick alert like "alert('hi there!');" before "Start Request" and after the display activating script, the loading div will appear in WebKit and will remain in sight for the duration of the request.

    What could I change to make WebKit display the div in the same way Firefox does?
    "I haven't failed, I just found
    100,000 ways that don't work"
    Thomas Edison

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    If you set the sjaxsubmitting element to be visible (without actually doing the request) does it show up?

    It could be that the browser screen hasn't been repainted by the time it gets to doing the request. Reading about the alert() thingy, it seems that's probably the problem. Not sure how you'd tackle that though. Maybe calling the sjax request after a small timeout (say, 100ms) might help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •