View Full Version : [AJAX] How to update image source?

08-03-2006, 01:36 PM
I'm currently using these functions to handle an Ajax response and update the page:

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

<a href="javascript:updatePage('status', 'unlocked.gif')">
<img id="status" src="locked.gif">

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?

08-03-2006, 03:36 PM
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().