Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent JQUERY .load from cache

Tags:

html

jquery

http

It seems that in IE9, using jquery .load will pull data from the browsers cache and so the 'real time' effect of AJAX loading content isnt working. Is there any kind of switch to force jquery .load to get the data fresh?

Thansk!

like image 947
Mark Avatar asked Jul 23 '11 18:07

Mark


3 Answers

The option to disable caching is available in jQuery if you are using the $.ajax low-level method. This is not represented in the simplified signature for load. You could mimic it instead, by ensuring that the URL is always unique. You can put the current time in milliseconds on the end of the URL to achieve that, since that will practically always be unique:

url += '?_=' + Date.now();

(This presumes that url is a string containing the request URL and that it doesn't have any query parameters currently.)

like image 182
lonesomeday Avatar answered Nov 02 '22 12:11

lonesomeday


Or you can use $.ajax and set the cache option to false which will cause jQuery to append a timestamp to the request URL under the hood:

$.ajax( {
    url: "foo.html",
    type: "GET",
    cache: false,
    success: function(html) {
        $("#foo").html(html);
    }
});
like image 26
karim79 Avatar answered Nov 02 '22 11:11

karim79


On the page content you try to load, make it to send header first so that request will not use cache...

For example, on the top of the code, add following lines

<?php
header("Cache-Control: no-cache");
header("Pragma: no-cache");

//Other page contents that have to be loaded

?>
like image 40
user482594 Avatar answered Nov 02 '22 10:11

user482594