简体   繁体   English

如何强制IE重新加载javascript?

[英]How to force IE to reload javascript?

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. 我在Vista上使用IE 8,每次更改javascript文件然后开始调试时,我必须按Ctrl + F5才能重新加载我的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? 有没有什么方法可以让我在开始调试时自动重新加载javascript,但是在浏览网页时不会失去性能提升?

Yeah yeah I know you probably don't like IE, but keep in mind the question isn't "What's the best browser?". 是的,我知道你可能不喜欢IE,但请记住问题不是“什么是最好的浏览器?”。

Add a string at the end of your URL to break the cache. 在URL末尾添加一个字符串以中断缓存。 I usually do (with PHP): 我经常做(用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. 如果您查看本网站的来源,他们会以类似的方式在网址末尾附加修订号,以便在更新javascript文件时强制更改。

Paolo's general idea (ie effectively changing some part of the request uri) is your best bet. Paolo的一般想法(即有效地改变请求uri的某些部分)是你最好的选择。 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). 当然,任何一个(正如我所描述的那样)都会涉及每次更新include语句,因此您可能想要采用动态方式来实现它,例如每次部署时用动态替换固定值(使用Ant或其他)。

When you work with web page or javascript file you want it to be reloaded every time you change it. 当您使用网页或javascript文件时,您希望每次更改时都重新加载它。 You can change settings in IE 8 so the browser will never cache. 您可以在IE 8中更改设置,以便浏览器永远不会缓存。

Follow this simple steps. 按照这个简单的步骤。

  1. Select Tools-> Internet Options. 选择工具 - > Internet选项。
  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. 单击确定按钮。

If you are looking to just do this on YOUR browser (aka 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. 而且我不会完全关闭IE上的缓存,因为您访问的每个网站都会失去性能提升。 You can tell IE to always refresh from the server for a specific site or browsing session: 您可以告诉IE始终从服务器刷新特定站点或浏览会话:

  1. Open IE Developer Tools 打开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.. 或者,对于键盘 - 快捷方式 - philes(像我一样)那里..

  • F12, Alt+C, Down Arrow, Alt+R. F12,Alt + C,向下箭头,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. 我们发现了一个问题,即IE缓存了我们的Ajax调用,而不是更新GET方法的结果。 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. 如果我们在IE中禁用缓存,我们就不会在开发中看到这个问题,并且会将它部署到这样的生产中。

This should do the trick for ASP.NET. 这应该是ASP.NET的技巧。 The concept is the same as shown in the PHP example. 这个概念与PHP示例中显示的相同。 Since the URL is different everytime the script is loaded, neither browser of proxy should cache the file. 由于每次加载脚本时URL都不同,因此代理浏览器都不应该缓存该文件。 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. 我习惯把我的JavaScript代码放到单独的文件中,并浪费了大量的时间用Visual Studio,直到我意识到它不会重新加载JavaScript文件。

<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. 显然,此解决方案应仅在开发阶段使用,并应在发布站点之前删除。

If you are running ASP.Net, check out the Bundling and Minification module available in ASP.Net 4.5. 如果您运行的是ASP.Net,请查看ASP.Net 4.5中提供的Bundling and Minification模块。

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

You can declare a "bundle" that points to your javascript file. 您可以声明一个指向您的javascript文件的“包”。 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. 它还可以将多个.js文件捆绑在一起,并将其缩小,一步到位。 Ditto for css. 同样适用于CSS。

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. 在javascript中我认为这是不可能的,因为现代浏览器在javascripts中有安全策略 ..并且清除缓存是一个非常违反的策略。

You can try to add 你可以尝试添加

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

In your header, but you will have performance loss. 在你的标题中,但你会有性能损失。

Add a date of modification of js file at the end of your URL. 在URL末尾添加js文件的修改日期。 With PHP it would look something like this: 使用PHP,它看起来像这样:

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. 每次更新脚本时都会重新加载脚本。

To eliminate the need to repeatedly press F5 in an IE tab while developing a website, use ReloadIt . 要在开发网站时无需在IE选项卡中重复按F5,请使用ReloadIt

在此输入图像描述

For each webpage displayed in IE, you can configure a filename, a directory, or a set of them. 对于IE中显示的每个网页,您可以配置文件名,目录或一组。 If any change occurs in any of those configured paths, ReloadIt refreshes the IE tab. 如果在任何这些配置的路径中发生任何更改,ReloadIt将刷新IE选项卡。 A simple tool. 一个简单的工具。 It just works. 它只是有效。

This will reload everything, not just javascript. 这将重新加载所有内容,而不仅仅是javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM