Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: AJAX refresh

  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts

    AJAX refresh

    Hi folks,

    I have an AJAX script to refresh a certain element in the page. The problem is that the element I need it to refresh is a dynamic image.. but the way I've done it inserts the raw image into the browser, thus just comes up with gobbledy gook characters and symbols. Would there be any way to do this? or would there be another approach?

    Code:
    function createRequestObject() {
    
       var req;
    
       if(window.XMLHttpRequest){
          // Firefox, Safari, Opera...
          req = new XMLHttpRequest();
       } else if(window.ActiveXObject) {
          // Internet Explorer 5+
          req = new ActiveXObject("Microsoft.XMLHTTP");
       } else {
          // There is an error creating the object,
          // just as an old browser is being used.
         alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
       }
    
       return req;
    
    }
    
    // Make the XMLHttpRequest object
    var http = createRequestObject();
    
    function sendRequest(page) {
    
       // Open PHP script for requests
       http.open('get', page);
       http.onreadystatechange = handleResponse;
       http.send(null);
    
    }
    
    function handleResponse() {
    
       if(http.readyState == 4 && http.status == 200){
    
          // Text returned FROM the PHP script
          var response = http.responseText;
    
          if(response) {
             // UPDATE ajaxTest content
             document.getElementById("captch").innerHTML = response;
          }
    
       }
    
    }
    
    function repeatloop()
    {
    sendRequest('captch1.php'); 
    setTimeout("repeatloop()", 2000);
    }
    
    window.onload=function() {
    repeatloop();
    }
    at the moment the image is in a 'span' element. If I change innerHTML to src, and the element to img, it puts the element src to

    "http://helraizer.dnsalias.net/cap/GIF87a%EF%BF%BD"
    is there anyway to change this with the code I have/another way?

    Sam

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Can't you just change the src of the image instead of getting the page with AJAX..?

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Mwnciau View Post
    Can't you just change the src of the image instead of getting the page with AJAX..?
    Possibly, but I wanted the image to change every 60 seconds (i know the code was set to 2), without having to refresh the entire page. Would that be possible?

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    var 0// to stop caching of the image
    setInterval(" document.getElementById('image').src='image.jpg?'+i;  i++; "60000); 
    Last edited by Mwnciau; 12-13-2007 at 04:26 PM.


  •  

    Posting Permissions

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