Based on the comments of the questioner, here is one of the possibilities of achieving the desired goal simply and effectively.
Create a PHP file called get-image.php
and put the following code inside it
<?php
if (isset($_GET['ajax'])) {
$numRand = rand();
echo "<img src='imagem.png?{$numRand}'>";
}
Now, create a PHP or HTML file, with the name you prefer and put the following code
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Burlando cache do navegador com AJAX</title>
</head>
<body>
<div id="imagem"></div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
function attImagem() {
$.ajax({
url: 'get-image.php',
type: 'GET',
data: 'ajax=null',
success: function(data) {
$("#imagem").html(data);
}
});
}
setInterval("attImagem()", 1000);
</script>
</html>
Explaining the logic
When you enter your main file (the one containing the HTML and Javascript code), jQuery will be included and a function will be triggered every second. This function will call via AJAX (without updating the page) the get-image.php file
The get-image.php file has the function of generating a random number and assigning the name of the image as a GET parameter, making the browser understand that that image is different, and not the same that was previously loaded.
Final considerations
- If you want to change the name of the image that will be requested, look for the
linha 5
of the archive get-image.php
, and change the.png image to the name of your image, as well as the path (if in another directory).
- In the above script, the image will be displayed in the call div
imagem
, if you want, you can change the name, but do this both in the div ID and within the AJAX function, where you have $("#imagem").html(data);
- To increase or decrease the interval between updates, change the value
1000 (em milissegundos)
.
- If it is an image, placing a very low value can cause the image not to be displayed and can also overload the server where the image is hosted.
Try to read it here, get help: http://stackoverflow.com/questions/7413234/how-to-prevent-caching-of-my-javascript-file
– Jhonathan
The answer to your question is: it is not possible to clear the browser cache using Javascript, browsers do not allow this. There are other ways to get what you want, many aliases. For example, you could play a different name for the image with each update, so you wouldn’t have cache, or you could use the GET parameters like this: scoring.png? v=1 and keep on changing the value of 1. I don’t know what your project is like, but I also believe that you don’t need an image, maybe you can do it with text, which would be better.
– Clayderson Ferreira
I will explain it better ( I have a photographic camera that snaps every 100 milliseconds and sends it to my computer, I would like to take these photos in real time and display on the site, would look like an animation ). But the machine always replaces the previous image, so I would like the site to do an update without refresh and display the current image, is there any way to do it ? What’s the right way to do it ?
– Desenvolvedor
I get it. I can do it and it’s not hard, but I have a question. Does the site have to update the image every 100 milliseconds as well? If this is in an intranet environment, maybe we can, but if it’s on the web, I don’t think we’ll have time to upload the image.
– Clayderson Ferreira
So I’m doing it locally.
– Desenvolvedor
It doesn’t seem logical, repeating a process like this every 100 milliseconds. If you can explain the context of this, the reason, etc., you can find a more appropriate solution. But if you want to continue like this, this can help to give an idea of how to always display the new image: http://answall.com/a/115436/4793
– Daniel Omine