php - New image is created but the old one is shown JS(AJAX) -


i have ajax function

function getoutput()  {    var ajax = getrequest();   document.getelementbyid('output').innerhtml = "<br/>";   ajax.onreadystatechange = function()   {       if(ajax.readystate == 4)       {           document.getelementbyid('output').innerhtml = ajax.responsetext;       }    }   document.getelementbyid('output').innerhtml = "<br/><img src=img/ajax-loader.gif><br/><br/>";   ajax.open("get", "graph_2.php", true);   ajax.send(null); }     

html

<form >   <input type="image" src=img/button.jpg class=mygtukas    onclick='getoutput(); return false;'> </form> <span id=output></span> 

which calls php code create graph. first time works fine, when user changes graph parameters , press button again (page doesn't reload) shows old graph dough there new graph created , saved in server.

this because of cache of browser. browsers saves copy of image on computer on in temp directory. because image has same name , location previous version, loads image disk, loads allot faster (no need download again)

the solution simple. change imagename variable. used trick add random value:

instead of image.jpg, return image.jpg?_randomstringhere

the randomstring can based on few things. in php use uniqid() because unique (unless can refresh faster 1 milisec). method (as suggested in comments below), timestamp. in php time() suffice.

if want javascript solution, can allong lines of image.src = image.src +'?'+ math.random()


it occurred me might ajaxcall itself. can use javascript suggestion stated above in ajaxcall make every call unique

math.random() documentation


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -