0

I have example of code below.

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

So, will my browser still cache this just by not setting this scripts headers meta tag cache to must-revalidate?

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
lock
  • 6,404
  • 18
  • 58
  • 76

3 Answers3

2

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
  • 56,416
  • 14
  • 107
  • 150
  • thanks for ETags which i wasnt familiar with upon asking this question hehe it did the trick – lock Nov 21 '08 at 02:28
0

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
  • 82,642
  • 24
  • 155
  • 189
0

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
  • 5,895
  • 7
  • 43
  • 64