tags:

views:

429

answers:

4

Hi you all masters. I need help since 2 days I'm looking all over the internet and couldn't find answer for my problem.

I have this jquery script to call an external file. So far so good. The script is working fine, but as always IE makes what he wants. The external file that I load with this script (weather.php) is a file with real-time weather conditions data in it. Whit this script, I can refresh the div inside which is my weather.php file. And obviously I don't want IE to cache the data in this file. I want when someone click on button "REFRESH", the included page to be reloaded with the new data in it. In IE this doesn't happens because of the cache. How can I change this script to not cache the div's content, or how can I say to my included file (weather.php) to do not cache it self?

This is the script:

function ajax_request() {
$('#column_weather').html('<img src="../images/home/ajax-loader.gif" width="16" height="11" style="vertical-align:middle;"/><b>&nbsp;&nbsp;Loading...</b>');
$('#column_weather').load("../includes/home/weather.php");

} `

And that's how I call the script:

<a href="#" onclick="ajax_request();return false;">Refresh</a>`
A: 

Can't you just have proper caching instructions inside this weather.php file (to say not to cache it)

DmitryK
What if he didn't have access to the contents of that file?
Jason Miesionczek
Then the answer would have been different. But he references this file from the local path
DmitryK
+1  A: 

Put a random variable on your query String

$('#column_weather').load("../includes/home/weather.php?myRand=" + guid());

I would make random var return a guid

function s4() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function() guid{
       return s4()+s4()+"-"+s4()+"-"+s4()+"-"+s4()+"-"+s4()+s4()+s4();
}
Zoidberg
beat me to it :)
Jason Miesionczek
:)
Zoidberg
Hi Zoidberg. Thanks for your opinion. I try your code, but when I added ?myRand=" + guid() and the next functions that you wright to me, the script stop working. The anchor started to return # in address bar, and stop refreshing the weather.php?
Spoonk
That is strange... is it something specific with the implementation of the php page? I have done this many times, and it is used by YUI to prevent caching as well. Maybe just try a random number and see.
Zoidberg
Jesus!!! Thanks to JoshJordan, who reminds me that I have FireBug, I saw that a pieces of yours code are not in places, and I corrected them. Now your solutions works just fine!!! Thanks, and... call the price :)))
Spoonk
Glad it worked... sorry the code was incomplete. I do now see a trailing bracket on the end.
Zoidberg
A: 

I would attach the current date and time as a GET parameter. Internet Explorer (and other browsers) view this information as critical to loading the page, just as any function returns a different value with different parameters. The trick is that you don't have to use the parameter. :)

$('#column_weather').load("../includes/home/weather.php?t=" + date());
JoshJordan
Hi Josh. Thanks for your opinion. I try your code, but when I added ?t=" + date(), the script stop his work. The anchor started to return # after URL in address bar, and stop refreshing the weather.php?
Spoonk
I think you're going to need to do some debugging. Fire up the FireBug :)
JoshJordan
Josh, thank you a lot, for this simple and obvious remind ;)))Now everything is fine :)
Spoonk
A: 

Adding a random parameter to the end of the query URL will help, but try adding this to the beginning of weather.php:

<?php 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate'); 
header('Cache-Control: post-check=0, pre-check=0', FALSE); 
header('Pragma: no-cache'); 
?>
nikc
Hi, thanks for advise. I already try this, but it doesn't help at all. The weather.php, continued to be cached from IE.
Spoonk