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 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question [AJAX] How to update image source?

    I'm currently using these functions to handle an Ajax response and update the page:
    Code:
    function handleResponse() {
      if (http.readyState == 4 && http.status == 200) {
        var response = http.responseText;
        var update = new Array();
    
        if (response.indexOf('|') != -1) {
          update = response.split('|');
          updatePage(update[0], update[1]);
        }
      }
    }
    
    function updatePage(div, text) {
      var viewer = document.getElementById(div);
      viewer.innerHTML = text;
    }
    If I want to update an image source, I'd use
    Code:
    <a href="javascript:updatePage('status', 'unlocked.gif')">
      <img id="status" src="locked.gif">
    </a>
    
    function updateImage(img, text) {
      var image = document.getElementById(img);
      image.src = text;
    }
    But now i need two functions. Is there a generic way of using just one function to update any type of content on the page?
    Last edited by Reveller; 08-03-2006 at 02:26 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd just take a line from JSON... JSON writes info. using js's object notation and then has the onreadystatechange function "load" it into the current page using eval(). Thus one possibility might be to have the server return js functions etc. in text format and just run them on the client using eval().
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

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