1

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?

11
  • Don't know what this does : Cache-Control: no-cache, max-age='3600' but browser is telling you that you should not use : in it Commented Oct 24, 2021 at 17:29
  • i want to tell the browser not to cache my site Commented Oct 24, 2021 at 17:31
  • 2
    That’s not a valid javascript syntax. What’re you trying to do here? Commented Oct 24, 2021 at 17:38
  • i want to prevent the browser from caching the website @Terry Commented Oct 24, 2021 at 17:39
  • 2
    This isn’t the right way to do it. Where did you get the snippet from? You shouldn’t be using JS to set these headers, since it’s already too late when the browser sees it anyway. Your server should be the one adding it. Commented Oct 24, 2021 at 17:40

1 Answer 1

3

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

Cache-Control configuration

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.

Apache

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>

Nginx

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";
}

PHP

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.

Sign up to request clarification or add additional context in comments.

5 Comments

i need it for alpha server or anywhere
I found a link on google see if you can understand : support.google.com/campaignmanager/answer/2837459?hl=en . As it is out of my scope
the php worked , thank you
No problem but you said you are not using php than how you managed to solve it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.