SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: AJAX gallery

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX gallery

    Hi,

    I've been working on an AJAX gallery recently, and I have a problem that I can't figure out (I'm quite new at javascript). What I want to happen is when the user clicks on a particular thing (a td cell in this case), it calls a script and recieves data from it (which javascript then processes and displays). The thing that happens with my script is that on the first click, nothing happens, but any click after that and the data seems to come through. Here's my script:
    Code:
    var ajaxgallerydata;
    function ajaxgallery(url) 
    {
       http.open('get', url, true);
       http.onreadystatechange = handleResponseGallery;
       http.send(null);
    }
    function handleResponseGallery() 
    {
    	if(http.readyState == 4 && http.status == 200)
    	{
    		var response = http.responseText;
    		if(response) 
    		{
    			ajaxgallerydata = response;
    		}
    	}
    }
    function switch_gallery(gallery)
    {
    	ajaxgallery('gallery.php?act=ajaxgallery&gallery=' + gallery);
    	if (ajaxgallerydata)
    	{
    		str = ajaxgallerydata.split('{<!splitter!>}');
    		document.getElementById('maindisplaywindow').innerHTML = str[0];
    		document.getElementById('photoscontainer').innerHTML = str[1];
    	}
    	str = null;
    }
    The str.split is used to process the data into 2 parts, which I believe is the problem. When I removed the if (ajaxgallerydata), I got an error saying that ajaxgallerydata had no properties, so then I made it output the data and it showed as null. Is there any fix to this?
    Also, is it possible to select all images in all td's on a page that have the same class?
    Thanks

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thie script is asynchronous, so yo should put this part

    Code JavaScript:
    if (ajaxgallerydata)
        {
            str = ajaxgallerydata.split('{<!splitter!>}');
            document.getElementById('maindisplaywindow').innerHTML = str[0];
            document.getElementById('photoscontainer').innerHTML = str[1];
        }

    in the function handleResponseGallery.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Hexburner, that fixed the AJAX problem.


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
  •