How to force IE to reload javascript?

后端 未结 10 1365
清酒与你
清酒与你 2020-12-12 20:41

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

相关标签:
10条回答
  • 2020-12-12 21:10

    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.
    0 讨论(0)
  • 2020-12-12 21:13

    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).

    0 讨论(0)
  • 2020-12-12 21:13

    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.

    0 讨论(0)
  • 2020-12-12 21:13

    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.

    0 讨论(0)
  • 2020-12-12 21:18

    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.

    0 讨论(0)
  • 2020-12-12 21:22

    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.

    0 讨论(0)
提交回复
热议问题