Alex
Alex

Reputation: 68406

PHP - Is dynamic CSS/JS cached by browsers?

I'm including javascript generated by php for each page, like

<script type="text/javascript" src="http://mysite.com/?get_the_js=1"></script>

Will this be cached by browser, so if you would go on another page some js might not work, because the previous page js is used?

If it is, how could I prevent caching?

Upvotes: 0

Views: 427

Answers (3)

Michael
Michael

Reputation: 1322

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

This should do it.

-michael

Upvotes: 1

Nican
Nican

Reputation: 7935

You are going to have to modify the HTTP headers to tell the browser not to cache the file.

Take a look at example 1: http://www.w3schools.com/php/func_http_header.asp

Upvotes: 1

Pekka
Pekka

Reputation: 449385

Will this be cached by browser

It depends on what caching headers your web server is configured to send for PHP scripts, if any. Usually, none are sent and no caching should take place.

You could use a tool like Firebug's "Net" tab to find out. If you want to make totally sure, see e.g. @fire's answer to this question to see how to disable caching completely from within PHP by sending the right headers.

Upvotes: 3

Related Questions