最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google chrome - Preventing Cache on JavaScript files - Stack Overflow

programmeradmin0浏览0评论

I'm trying to prevent 2 JavaScript files from being cached by the browser.

I've tryed to use <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> without success. Here's my <head> element code:

    <head>

<meta charset="UTF-8">
<meta http-equiv="Cache-control" content="NO-CACHE">

<link type='text/css' href='/files/theme/popup_basic.css' rel='stylesheet' media='screen' />

<!-- JavaScript Start -->
<script type="text/javascript" src="/files/theme/gohome.js"></script>
<script type="text/javascript" src="http://192.168.0.149/redirect.js"></script>
<!-- JavaScript End -->

</head>

From my understating, this should work. But the redirect.js file keeps being cached!

Anyone know what I'm doing wrong?

I'm trying to prevent 2 JavaScript files from being cached by the browser.

I've tryed to use <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> without success. Here's my <head> element code:

    <head>

<meta charset="UTF-8">
<meta http-equiv="Cache-control" content="NO-CACHE">

<link type='text/css' href='/files/theme/popup_basic.css' rel='stylesheet' media='screen' />

<!-- JavaScript Start -->
<script type="text/javascript" src="/files/theme/gohome.js"></script>
<script type="text/javascript" src="http://192.168.0.149/redirect.js"></script>
<!-- JavaScript End -->

</head>

From my understating, this should work. But the redirect.js file keeps being cached!

Anyone know what I'm doing wrong?

Share Improve this question asked Mar 7, 2013 at 17:26 Ryan FitzgeraldRyan Fitzgerald 4554 gold badges10 silver badges20 bronze badges 2
  • 2 Why would you want to prevent caching of the file? If you're generating the HTML on the server, you could append a GUID to the src attribute, resulting in a unique "file" like this: <script type="text/javascript" src="http://192.168.0.149/redirect.js?C6324B81-A31F-4167-81AE-AD51F239C328"></script> – Bernhard Hofmann Commented Mar 7, 2013 at 17:30
  • Because this file is a redirect and it only needs to be redirected when the server is online. When it is offline, the user will stay at that page. – Ryan Fitzgerald Commented Mar 7, 2013 at 17:37
Add a comment  | 

1 Answer 1

Reset to default 15

The <meta http-equiv="Cache-control" content="NO-CACHE">, the directive CACHE-CONTROL:NO-CACHE indicates cached information should not be used and instead requests should be forwarded to the origin server.

In order to prevent cache on every request, you may need to add some random string in url. The example below is use javascript to dynamic create a script tag and adding random number in the url, then append it.

<script language="JavaScript">
 var s=document.getElementsByTagName('script')[0];
 var sc=document.createElement('script');
 sc.type='text/javascript';
 sc.async=true;
 sc.src='http://192.168.0.149/redirect.js?v' + Math.random(); 
 s.parentNode.insertBefore(sc,s);
</script>

If just want to prevent 1 time only, just append some string to the src.

<script src="http://192.168.0.149/redirect.js?12345678"></script>
发布评论

评论列表(0)

  1. 暂无评论