Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yg17

macrumors Pentium
Original poster
Aug 1, 2004
15,030
3,009
St. Louis, MO
So, the basics of what I'm attempting to accomplish, is the user submits a form, using AJAX, it's updated, and a row is added to the table. Works great in all browsers, Microsoft's piece of crap included.

After the form is submitted, and the table is updated, a balance that is elsewhere on the page needs to also be updated. It's basically kinda like a bank register. Add a transaction, the balance is automatically calculated. So, I have this code:

Code:
function updateBalance(account) {
    reqBal = openAjax();
    var url = "ajax.php?act=balance&id="+account;
    reqBal.open("get", url, true);
    reqBal.onreadystatechange = handleBalance;
    reqBal.send('null');
    
}

function handleBalance() {
    if(reqBal.readyState == 4){
        alert(reqBal.responseText);
        document.getElementById('balspan').innerHTML = reqBal.responseText;
        
    }
}

updateBalance() is called from the function that submits the stuff to the PHP script to be added to the DB. openAjax() returns an XMLHTTPRequest (and the IE equivalent). Ajax.php calculates the new balance and does nothing else but print out the balance formatted with currency symbol and decimals, and this is captured by reqBal.responseText, as you would expect. This code works perfectly in all Mac browsers. So, I fire up my PC, launch IE and test. The very first time the balance needs to be updated, it works. After that, IE refuses to change the value of responseText next time the function is called....it's basically stuck on the first value it gets when it's first called. Any future calls it ignores and thus, the balance doesn't update. The only way to make it take a new value is to completely close all of IE's windows, open it back up and do it again. It will get a new value, then get stuck on that.

So, can someone tell me what I screwed up, or some other obnoxious, lengthy, complicated work around to make IE work as it should? Because right now, if IE had them, I'd kick it square in the balls. Thanks
 
bootedbear said:
Does it work as expected in other Windows browsers?

(And where's the Java part? Did you mean Javascript?)


Yeah, I meant JavaScript. It's been a long night.

Haven't tested it in other Windows browsers yet. Works fine in FireFox for OSX, so my guess would be it would work fine in FF for Windows, but I'll eventually test it there to make sure
 
Found the problem. IE was caching the result it got from AJAX, so instead of requesting the page again, it would go to the cache and insert that into responseText, thus the reason for it not changing unless I closed the browser (or clearing temp files i guess). I fixed it by adding a random number to the end of the query string in var url = "ajax.php?act=balance&id="+account; So as long as Javascript doesn't generate the same number twice in a browser session (and it's set to generate between one and 1,000,000,000) then it's OK.

Really dumb on Microsoft's part. Caching the result defeats the whole purpose of AJAX.
 
yg17 said:
Really dumb on Microsoft's part.

And you're surprised? :cool:

Caching would have been my guess had you tried it successfully in other Windows browsers. If it had not worked in other browsers then it would probably have been a setup issue.
 
yg17 said:
Found the problem. IE was caching the result it got from AJAX, so instead of requesting the page again, it would go to the cache and insert that into responseText, thus the reason for it not changing unless I closed the browser (or clearing temp files i guess). I fixed it by adding a random number to the end of the query string in var url = "ajax.php?act=balance&id="+account; So as long as Javascript doesn't generate the same number twice in a browser session (and it's set to generate between one and 1,000,000,000) then it's OK.

Really dumb on Microsoft's part. Caching the result defeats the whole purpose of AJAX.

I don't really know much about AJAX but rather than do a nasty hack with random numbers why don't you append this value you are wanting to update to the end of the query? That way if it changes, it won't use the cache, and if the number is the same it won't matter if it uses the cache?

Sorry if this doesn't make sense though, might have misinterpreted the question.
 
Wes said:
I don't really know much about AJAX but rather than do a nasty hack with random numbers why don't you append this value you are wanting to update to the end of the query? That way if it changes, it won't use the cache, and if the number is the same it won't matter if it uses the cache?

Sorry if this doesn't make sense though, might have misinterpreted the question.

It's not sending a value to PHP to update a database, it's receiving a value from PHP to update the page.
 
I don't remember having any problems with caching when I was writing MacRumorsLive, as long as the correct HTTP headers were set. However, it is using randomised URLs because Safari 1.2 (I think it was 1.2 anyway) refused to do more than one request on the same URL within the same page load...
 
I have the same problem with caching on Flash with XML loading over SSL, I managed to resolved that using the following php header, not sure if this will help in this case.

Pragma:no-cache
Cache-control:no-cache
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.