1
div.load('test.txt');

Result - lorem ipsum - correct

Now, after I change the content to dolor sit

div.load('test.txt');

result - lorem ipsum - incorrect

Obviously, there is a problem with caching content.
How does one solve this?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • Usually you can add a timestamp like `test.txt?Cache=123`. Never tried that with `load()` tho. Like `"test.txt?Cache=" + +new Date`. – Lain Jun 25 '20 at 17:46
  • tried and getting error - `unrecognized expression` – qadenza Jun 25 '20 at 17:51

1 Answers1

2

You can add a random query string each time, so the content is not cached. The current date should be unique enough for this purpose.

div.load('test.txt?query=' + Date.now());
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • `Date.now()` is pretty much enough. You do not need a new version all the time, merely the newest. Unless someone saves changes faster than in 1ms and someone else requests it at exactly that time, you are safe enough. – Lain Jun 25 '20 at 17:56
  • it works, thanks. Is there a way to turn off js cache funcionality entrirely on a web page, pls? – qadenza Jun 25 '20 at 18:04
  • @qadenza Just don't add the query string on that specific page, i.e. just use `div.load('test.txt');` – Unmitigated Jun 25 '20 at 18:06
  • but in that case the new content is not loaded. Seems you didn't understand me. Your solution works if cache is turned on, but maybe is better to turn it off, so there is no need for query string? – qadenza Jun 25 '20 at 18:09
  • @qadenza I don't think you can modify users' browser cache settings, so using a query string is probably the simplest solution. – Unmitigated Jun 25 '20 at 18:10
  • 1
    In query you CAN turn cache for its ajax calls, but all that does is simply add a number to the query string everytime it is ran, which is what this solution does. https://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached – imvain2 Jun 25 '20 at 18:13