44

I'm using IE 8 on Vista, and everytime I change a javascript file and then start debugging, I have to hit Ctrl+F5 to have it reload my javascript. Is there any way to make it automatically reload javascript when I start debugging, but not lose the performance gains when just browsing the net?

Yeah yeah I know you probably don't like IE, but keep in mind the question isn't "What's the best browser?".

Max Schmeling
  • 12,363
  • 14
  • 66
  • 109

10 Answers10

48

Add a string at the end of your URL to break the cache. I usually do (with PHP):

<script src="/my/js/file.js?<?=time()?>"></script>

So that it reloads every time while I'm working on it, and then take it off when it goes into production. In reality I abstract this out a little more but the idea remains the same.

If you check out the source of this website, they append the revision number at the end of the URL in a similar fashion to force the changes upon us whenever they update the javascript files.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • Good option, only it means that browsers will _never_ cache the file meaning you "lose the performance gains when just browsing the net". – Alconja May 15 '09 at 01:23
  • 5
    You lose the performance gains when "just browsing the website you're currently working on" more like. – Paolo Bergantino May 15 '09 at 01:25
  • Ok, I assumed you meant to go live with this. (remember that end users will have cache issues as well, so its not a bad idea to make sure that when you .js files change the user's browser somehow knows to get the latest version too). – Alconja May 15 '09 at 01:39
  • What do you do to have it fixed in production so it is not reloaded every time? Changing all the related pages when the javascript file is updated. Without committing any unneeded change to the pages in the repository. – Eduardo Feb 10 '10 at 21:14
  • 3
    As @Paolo suggested above, just append the revision number to the end of the URL. somescript.js?r=2011.07.24 or whatever. You could also append an encoded version of the last modified date. This allows the browser to only cache the latest version of your script. When the script is updated, the URL changes, so the browser fetches the file again. – BMiner Jul 24 '11 at 13:12
  • how would i incorporate that line in my code please? i also have a refresh problem, in my .php file i have at the top: and also have a rainbow.js file thanks – charlie_cat Jul 25 '11 at 14:09
  • I think it would be better to define a variable for version id, then use this variable at the end of including the js file. this way the javascript file will be reloaded only when the developer wants it to be, by simply changing the value of the version variable. – Luci Jun 21 '12 at 08:35
  • @PaoloBergantino Do you know how often IE 10 clears its cache without user input? I thought it might be every 24 hrs. Am I wrong? – Mr. Concolato Oct 01 '14 at 16:40
25

Paolo's general idea (i.e. effectively changing some part of the request uri) is your best bet. However, I'd suggest using a more static value such as a version number that you update when you have changed your script file so that you can still get the performance gains of caching.

So either something like this:

<script src="/my/js/file.js?version=2.1.3" ></script>

or maybe

<script src="/my/js/file.2.1.3.js" ></script>

I prefer the first option because it means you can maintain the one file instead of having to constantly rename it (which for example maintains consistent version history in your source control). Of course either one (as I've described them) would involve updating your include statements each time, so you may want to come up with a dynamic way of doing it, such as replacing a fixed value with a dynamic one every time you deploy (using Ant or whatever).

Alconja
  • 14,834
  • 3
  • 60
  • 61
  • Will work in Firefox and Chrome too? – BrianK May 09 '11 at 14:21
  • I am going to take this idea and try in my page load event (in .Net) to create a version number based on the date time (kind of a mixture of both you and Paolo's idea) because this is becoming a real headache. Not necessarily for me personally but when I update the JS to our testers, they forget to CTRL F5 and then send back bugs after they have been fixed... (just need to make sure I take this out before production!) – Todd Vance Nov 01 '11 at 15:38
  • THIS WORKS LIKE A CHARM: String jsDateTime = DateTime.Now.ToString().Replace(" ", "").Replace("/", "").Replace(":", ""); LiteralControl jsMain = new LiteralControl(); jsMain.Text = ""; Page.Header.Controls.Add(jsMain); – Todd Vance Nov 01 '11 at 16:26
  • `` thsi does not work for IE 11. – Afnan Ahmad Aug 31 '18 at 11:20
18

When you work with web page or javascript file you want it to be reloaded every time you change it. You can change settings in IE 8 so the browser will never cache.

Follow this simple steps.

  1. Select Tools-> Internet Options.
  2. In General tab click on Settings button in Browsing history section.
  3. Click on "Every time I visit the webpage" radio button.
  4. Click OK button.
Vadim
  • 21,044
  • 18
  • 65
  • 101
6

If you are looking to just do this on YOUR browser (a.k.a. not forcing this on your users), then I would NOT set your code to not utilize the cache. As José said, there is a performance loss.

And I wouldn't turn off caching on IE altogether, because you lose that performance gain for every website you visit. You can tell IE to always refresh from the server for a specific site or browsing session:

  1. Open IE Developer Tools
  2. Choose Cache from the Menu
  3. Click to check "Always Refresh From Server"

Or, for the keyboard-shortcut-philes (like myself) out there..

  • F12, Alt+C, Down Arrow, Alt+R.

NOTE OF CAUTION : Having said this... Keep in mind that anytime you develop/test in a different way than the site will be viewed in production, you run the risk of getting mixed results. A good example is this caching issue. We found an issue where IE was caching our Ajax calls and not updating results from a GET method. If we HAD disabled caching in IE, we would have never seen this problem in development, and would have deployed it to production like this.

undeniablyrob
  • 1,339
  • 2
  • 16
  • 16
  • On your "Note of Caution", this is why many dev shops have a special testing environment, where the goal is to mirror production as much as possible. For example, if you develop/test on Windows, but your production server is Linux, then you should have another test environment that runs on Linux. – michaelok Aug 15 '13 at 20:32
  • The item 'F12...' works for me, IE 8. Doesn't work for IE11. – youngzy Aug 03 '17 at 07:06
  • 1
    IE 11 equivalent: `Tools > Internet Options > General > Browsing History > Settings > Temporary Internet Files > Check for newer versions of stored pages: Every time I visit the webpage` – paulsm4 Apr 04 '18 at 03:55
2

If you are in IE11, you can tell IE to always refresh from the server for a specific browsing session with IE Developer Tools.

  • Open IE Developer Tools with F12 key
  • Go to Network tab
  • Click on button "always refresh from the server"

button always refresh from the server

(Sorry for french capture)

binard
  • 1,726
  • 1
  • 17
  • 27
1

This should do the trick for ASP.NET. The concept is the same as shown in the PHP example. Since the URL is different everytime the script is loaded, neither browser of proxy should cache the file. I'm used to put my JavaScript code into separate files, and wasted a significant amount of time with Visual Studio until I realized that it wouldn't reload the JavaScript files.

<script src="Scripts/main.js?version=<% =DateTime.Now.Ticks.ToString()%>" type="text/javascript"></script>

Full example:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

   <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>  
   <script src="Scripts/main.js?version=<% =DateTime.Now.Ticks.ToString()%>" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function () {
                                myInit();
                          });

    </script>
</asp:Content>

Obvously, this solution should only be used during the development stage, and should be removed before posting the site.

  • That would force to reload the script everytime. Instead of the current time, simply use the time of the application like `(new FileInfo(Assembly.GetExecutingAssembly().Location)).LastWriteTime.ToString("yyyyMMddhhmm");`. – akop Mar 20 '18 at 08:41
1

Add a date of modification of js file at the end of your URL. With PHP it would look something like this:

echo '<script type="text/javascript" src="js/something.js?' . filemtime('js/something.js') . '"></script>';

When your script will be reloaded every time you update it.

o_O
  • 516
  • 2
  • 8
  • 25
1

If you are running ASP.Net, check out the Bundling and Minification module available in ASP.Net 4.5.

http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

You can declare a "bundle" that points to your javascript file. Every time your file changes, it will generate a new querystring suffix... but will only do so when the file changes. This makes it super simple to deploy updates, because you don't even have to think about updating your tags with new version numbers.

It can also bundle multiple .js files together into one file, and minify them, all in one step. Ditto for css.

Rob Hudson
  • 11
  • 1
  • If someone is going to downvote this seemingly ideal solution, they should probably comment to say why. This is exactly what I'm looking for, a way to allow the browser to cache the files in general, but forces it to reload them when a change is made. – Jason Kelley Aug 09 '19 at 16:30
1

In javascript I think that it is not possible, because modern browsers have a policy on security in javascripts.. and clearing the cache is a very violating one.

You can try to add

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

In your header, but you will have performance loss.

José Leal
  • 7,989
  • 9
  • 35
  • 54
0

To eliminate the need to repeatedly press F5 in an IE tab while developing a website, use ReloadIt.

enter image description here

For each webpage displayed in IE, you can configure a filename, a directory, or a set of them. If any change occurs in any of those configured paths, ReloadIt refreshes the IE tab. A simple tool. It just works.

This will reload everything, not just javascript.

Cheeso
  • 189,189
  • 101
  • 473
  • 713