views:

3940

answers:

5

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?".

+7  A: 

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
Good option, only it means that browsers will _never_ cache the file meaning you "lose the performance gains when just browsing the net".
Alconja
You lose the performance gains when "just browsing the website you're currently working on" more like.
Paolo Bergantino
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
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
+1  A: 

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
That doesn't do what you think it does. See www.enhanceie.com/redir/?id=httpperf for a discussion of each of the settings.
EricLaw -MSFT-
+1  A: 

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
+3  A: 

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
A: 

"PRAGMA" tag seems to be working great! Thanks so much!

Margarita