Jquery script problem - cache not clearing

I’m using the following code to check for the existence of a file on the web server every 10 seconds. The file on the server is generated by a cron job, but only if conditions are right. Once the script finds the file, it sends the browser to a new page (warn.php) which displays some information in the browser and removes the file from the server.

setInterval ( "scanfolder()", 10000 );
	function scanfolder(){
            $(function()
            {
                $("#checkin").ajaxComplete(function(request, settings){
                      if (settings.status===200){
				window.location.href='http://localhost/warn.php'
                      }
            });
            $("#checkin").load("http://localhost/check_file")
        });
}

The problem I am having is that once the script finds check_file and forwards the browser to warn.php which removes the file, the user then clicks a link to go back to the orginal page and the script keeps acting like it’s still finding check_file (forwards the browser to warn.php over and over). If I clear the browser cache manually, it starts working properly again. I thought I could fix this problem with

<meta http-equiv="expires" value="Thu, 16 Mar 2000 11:00:00 GMT" /><meta http-equiv="pragma" content="no-cache" />

tags in the involved pages but no dice. What is going on here…?

BTW I don’t know if this is a factor but “checkin” is a div element on the main page. I didn’t really want the script tied to a div element but was just using some sample code and wasn’t sure how to modify it to be independent of any element.

The file which you are checking to see if exists should also be returning no-cache and expiry headers (not meta tags, just headers).

Is it doing that?

Yes - well I was trying to anyhow. I was trying the meta tags and that wasn’t working. I got it working properly now I think by using PHP’s header function.

The check_file is generated by a shell script so it’s just a little weird to be writing up a php file with it… no harm I guess… But is there just some text I can append to the file for a raw no-cache header?

Not really, as PHP is a server side extension of your web server it’s actually sending commands to the web server to send those headers.

You could lookup and see if there’s an extension for your web server where you can specify headers for specific files / extensions / regular expression file matches. If I was a web server developer I’d probably write something like that.

Oh look! Apache totally has one - http://httpd.apache.org/docs/2.0/mod/mod_headers.html

If you’re using another web server, they most likely have one too.

Hope that helped :smiley:

Even simpler, simply attach a nocache query string param to the query! Can’t believe I forgot this :@. Just pop a random number on the end - voila, unique ‘file’ query.

testfile.ext?rand=9348328489302

Hmm, so throw a random query string into the URL calling the file and as long as that changes each time it is loaded, there will be no caching?

Anyhow, Thanks for the pointers!

Yup, it’s quite common.

We do it with our frameworks here, put the version number at the end of the css or js bundle, so cached scripts from different versions don’t cause issues.