image src doesnt change after upload
-
06-07-2019 - |
Question
i am making an upload with Ajaxupload plugin and i am using this function in OnComplete event of ajaxupload;
function degis(){
var a = "<?php echo $id; ?>";
document.getElementById("imga").src = "../artwork/"+a+"/logo.jpg?dummy=371662";
document.getElementById("imga").style.width = "500px";
document.getElementById("imga").style.height = "175px";
}
but new uploaded image doesnt appear for a reason. i tried that "?dummy=371662" but didnt work.
i am also using this for Onsubmit event of ajaxupload
function updeg(){
var a = "uploading.gif";
document.getElementById("imga").style.width = "50px";
document.getElementById("imga").style.height = "50px";
document.getElementById("imga").src = a;
}
</script>
this is the html of this element
<img id="imga" alt="" height="175px" src="../artwork/<?php echo $id; ?>/logo.jpg?dummy=371662" width="500px">
Any suggestions on this ?
Solution
Based on your edits and comments above, I think you need something like this:
function junk() {
return (new Date()).getTime() + Math.round(Math.random());
}
function degis() {
var img = document.getElementById("imga");
if (img) {
img.src = "../artwork/<?php echo $id; ?>/logo.jpg?nocache=" + junk();
img.style.width = "500px";
img.style.height = "175px";
}
}
Your previous attempt to bypass the cache doesn't work because your "dummy" value is the same each time. By use of a junk()
function, as above, you get a different random value each time, ensuring that the image cannot be cached.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow