[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loading image for async call

Posted on 2007-10-11
5
Medium Priority
?
166 Views
Last Modified: 2013-11-19
I'm using an XMLHttpRequest call and want to display a loading or similar animation image while waiting.  Here's the psuedo code I'm using:

xReq = new XMLHttpRequest()
xReq.open(...)
xReq.onreadystatechange = function(){..poll state..}
text = xReq.responseText

Is there a pattern for displaying the image?  Any code examples?
0
Comment
Question by:brettr
  • 3
5 Comments
 
LVL 11

Expert Comment

by:MacAnthony
ID: 20059490
I don't know of examples using just the core implementation of javascript, but there I have seen several toolkits and js frameworks that make this implementation pretty easy. The one I've been looking at the most was Ext JS. It also has the benefit of having an AJAX module built in as well so you don't have to use the XMLHttpRequest function. Right now, this function is pretty browser specific (certain versions of IE if not all versions don't support this function) and it's easier to let a library deal with all that.

An example of it with code can be found:
http://extjs.com/deploy/dev/examples/form/xml-form.html
0
 
LVL 16

Accepted Solution

by:
Kiran Paul VJ earned 2000 total points
ID: 20059631
try this

//your code
xReq = new XMLHttpRequest()
xReq.open(...)
xReq.onreadystatechange = RequestHandler;

function RequestHandler()
{
      if (xReq.readyState == 4 && xReq.status == 200)
       {
            text = xReq.responseText;            
      }
        else
        {
                 document.getElementById('show_image').innerHTML = '<img src="loading.jpg" />';
        }
}


and in your
<body>
<div id="show_image"></div>
</body>


or

you can do like this

xReq = new XMLHttpRequest()
xReq.open(...)
xReq.onreadystatechange = function () {
      if (xReq.readyState == 4 && xReq.status == 200)
       {
            text = xReq.responseText;            
      }
        else
        {
                 document.getElementById('show_image').innerHTML = '<img src="loading.jpg" />';
        }
}
0
 

Author Comment

by:brettr
ID: 20061766
How do I get the div to appear basically in the middle of the page on top of everything else?  Is there a way to gray out the stuff behind this div?
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20063100
try something like this to show the div on center of page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style>
#outerdiv {position:absolute;top:50%;left:50%;overflow:show;}
#innerdiv {position:absolute;top:-(50% of your height);left:-(50% of your width);}

</style>
</head>

<body>

<div id="outerdiv">
<div id="innerdiv">
<p>Hello world</p>
</div>
</div>
</body>
</html>
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question