0

I have made this script that, if I have internet (checking with image) then it redirects, but it keeps images in cache, and if I have no internet then it still loads.

<img src="http://www.example.com/image.png"
   onload="window.location='http://www.example.com/index.html';"
   onerror="window.location='error.html';" 
width="0px" height="0px"><br>Loading...

The problem is the cache, it caches the image and then when I reload without internet it still tries to load. Does anyone know another way to fix it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    to avoid cache, you have to use an other url, like add a random GET parameter at the end., see : http://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images – Hacketo Feb 05 '15 at 17:59
  • 1
    `http://www.example.com/index.html?1` is also a valid URL which points to the same index.html – anoopelias Feb 05 '15 at 18:00
  • it must only be Javascript and HTML –  Feb 05 '15 at 18:01

2 Answers2

0

Append a random number to the img url so that the browser doesn't cache the image. See

Disable cache for some images

Community
  • 1
  • 1
Joe W
  • 2,773
  • 15
  • 35
0

You could try something like this :

<body onload="myimg.src='http://www.example.com/image.png?'+(+new Date())">

    <img id="myimg"
       onload="window.location='http://www.example.com/index.html';"
       onerror="window.location='error.html';" 
       width="0px" height="0px">
</body>

The point is to add a random/unique value at the end of the url so the request is never the same

Hacketo
  • 4,978
  • 4
  • 19
  • 34