Cache Prevention in JavaScript Files

I am trying to prevent the caching of 2 JavaScript files in the browser.

I tried using <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> without success. Here is 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 understatement, this should work. But the redirect.js file is cached!

Does anyone know what I'm doing wrong?

+7
source share
1 answer

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

To prevent cache on every request, you might need to add some random string to the url. The following is an example of using javascript to dynamically create a script tag and add a random number to the url, then add 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 you want only 1 time, just add some line to src.

 <script src="http://192.168.0.149/redirect.js?12345678"></script> 
+11
source

All Articles