Results 1 to 3 of 3

Thread: undefined - Ajax image loader gif

  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default undefined - Ajax image loader gif

    Hello,

    I have been trying to get an Ajax script to have a preloader gif run while
    the framesets load. No matter what I do it remains undefined and I was looking for help to solve this problem. (I got your DD ajaxcars to work with your loader image fine) but can't do my own. Thank you if you can help.

    http://www.thewordgift.com/cisw400/a...xtest/new.html

    http://www.thewordgift.com/cisw400/a...jaxtest/new.js

    http://www.thewordgift.com/cisw400/a...t/mystyles.css
    Last edited by refreshingcode; 01-07-2011 at 10:51 PM. Reason: ???

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It's not the image that's undefined, rather outMsg2 - in the code here:

    Code:
    function useResponse()
    {
    
    	if(req2.readyState == 4 && req2.status == 200)
        	
            {
    		var outMsg2 = req2.responseText;
    		}
        	
    	else
        	{
    	document.getElementById('updateArea2').innerHTML = '<img src=ajaxloader.gif>';
    
        	}
    
    document.getElementById("updateArea2").innerHTML = outMsg2;
    
    }
    Regardless of whether or not the request is completed and successful, the last thing the function does is fill updateArea2 with outMsg2. Unless the request is completed and successful, outMsg2 is 'undefined'. Rewrite the function like so:

    Code:
    function useResponse()
    {
    	var outMsg2 = '<img src=ajaxloader.gif>';
    	if(req2.readyState == 4 && req2.status == 200)
            {
    		var outMsg2 = req2.responseText;
    	}
    	document.getElementById("updateArea2").innerHTML = outMsg2;
    }
    Now the output will always be the loading image, unless the request is completed and successful. Then it will be the contents of the request's response.

    Note: The useResponse() function could/should be augmented to deal with other req2.readyState s and other req2.status es. But like I've just made it, should be fine in most cases.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you John! A Javascript wizard!
    Last edited by refreshingcode; 01-09-2011 at 08:54 AM. Reason: short and sweet!

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
  •