1

I'm trying to load a page and put it inside a div.

code :

 $("#test").load("page.php");

The problem is that once it's loaded, sometimes when I re-execute this script, it seems that the file 'page.php' is not really reloaded, seems that this file is keep in a cache or something like this.

It's really a problem because I have a PHP request inside this file it doesn't execute it.

LeSam
  • 1,235
  • 4
  • 18
  • 38

3 Answers3

4

The browser is caching the request. It thinks it knows the result it will get, so it doesn't send a request.

To fix this, add a dynamic get parameter to the end of the url. This will trick the browser into expecting something new each time.

Here's an example code:

$("#test").load("page.php?v=" + Math.random());
ColBeseder
  • 3,579
  • 3
  • 28
  • 45
3

You can force not to cache it in the ajaxSetup and reset it back when not required. .load is just an ajax call and cache is defaulted to true.

$.ajaxSetup ({
    cache: false
});
PSL
  • 123,204
  • 21
  • 253
  • 243
2

It is a cache problem. You could change the ajaxsetup, or do something with a random string.

The reason the caching happends is because you request the same url twice. It has allready fetched that result, so it's just gets the cache's value. The simple trick is to add someting random:

$("#test").load("page.php?_"+Math.random());

I use underscore to it because doesn't overwrite any other get values (jQuery does it about the same way)

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • For my own benefit, why do the other posts get upvotes, and I dont? I don't see any information which is alot better than my reply. For my future posts, what didn't I do right? – Martijn Oct 04 '13 at 09:12