lock
lock

Reputation: 6614

Will a script in html's script tag with extension php be cached?

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?

Upvotes: 0

Views: 2790

Answers (3)

Magnus Smith
Magnus Smith

Reputation: 5963

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.

Upvotes: 0

Kent Fredric
Kent Fredric

Reputation: 57354

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.

Upvotes: 2

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

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().

Upvotes: 0

Related Questions