views:

404

answers:

3

for example <script type="text/javascript" src="assets/scripts/somescript.php"></script> will my browser still cache this just by not setting this scripts headers meta tag cache to must-revalidate?

A: 

If you send a Content-type: text/javascript; charset="your_charset" the browser will recognize your PHP script as a valid Javascript resource and will handle it like any other Javascript. You can control browser caching behavior by issuing the correct headers in your PHP script using header().

Stefan Gehrig
+2  A: 

Some browsers are more agressive with default caching than others. However, there are cache control headers you can send to indicate when to reload the code.

header("Expires: " . date("r", time() + ( 60 * 60 * 24 * 7 * 1 ) ) ); // Expires in 1 week
header("Content-Type: application/x-javascript");

Is a code-snippet I've been known to use.

You can use more fancy stuff like If-Not-Modified headers and ETags, but Expire times are the only ones that eliminate extra server calls.

Kent Fredric
thanks for ETags which i wasnt familiar with upon asking this question hehe it did the trick
lock
A: 

One trick is to write your script tag out with an ever-changing querystring on it. Your main PHP could write out the following, which changes each day:

<script type="text/javascript" src="assets/scripts/somescript.php?date=20081118"></script>

The querystring will be ignored by somescript.php, but the browser will treat the URL as a new one each time, and reload the script.

Magnus Smith