Reputation:
I want to use Javascript to tell the browser not to cache my site. The code written on this JavaScript file is as simple as
function calcURL(urlIn)
{
Cache-Control: no-cache, max-age='3600'
}
The browser keeps giving me an error:
Uncaught SyntaxError: Unexpected token ':'
I have tried adding ; at the end of the line. Can someone tell me what the problem is?
Upvotes: 1
Views: 7540
Reputation: 2544
You have used wrong method in JavaScript. Also it will be too late until JS run to tell browser not to cache .
You can read this for more about cache control
The HTTP Cache-Control header can be implemented on the server or can even be added within the code. The following are examples of how to implement Cache-Control in Apache, Nginx, or within your PHP code.
The following snippet can be added to your .htaccess file to tell the server to set the Cache-Control header's max-age to 84600 seconds and to public for the listed files. Expires and Cache-Control headers can also be included with Apache by using the mod_expires module.
<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=84600, public"
</filesMatch>
This snippet can be added to your Nginx configuration file. The example below uses the Cache-Control header directives public and no-transform with an expire setting set to 2 days.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 2d;
add_header Cache-Control "public, no-transform";
}
Cache-Control headers can also be added directly in your code. This example demonstrates using the PHP header to include Cache-Control setting a max-age of 1 day.
header('Cache-Control: max-age=86400');
Summary
Cache-Control is a powerful HTTP header when it comes to speeding up websites with the use of browser and intermediary cache. Although its ability to increase website speed is not it's only as it is also quite useful to help make private information less vulnerable. The settings you choose to apply to the Cache-Control directives are dependent on the nature of information that is being delivered as well the desired expiration time of those assets.
Upvotes: 3