有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?

使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。


当前回答

这是我在一个使用PHP的应用程序中使用的简单解决方案。

所有JS和CSS文件都放在一个有版本名的文件夹中。示例:"1.0.01"

root\1.0.01\JS
root\1.0.01\CSS

在那里创建一个Helper并定义版本号

<?php
function system_version()
{
    return '1.0.07';
}

和链接JS和SCC文件如下

<script src="<?= base_url(); ?>/<?= system_version();?>/js/generators.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<?= base_url(); ?>/<?= system_version(); ?>/css/view-checklist.css" />

每当我对任何JS或CSS文件进行更改时,我都会在Helper中更改System Verson并重命名文件夹并部署它。

其他回答

我也遇到过类似的问题,我是这样解决的:

In index.html file I've added manifest: <html manifest="cache.manifest"> In <head> section included script updating the cache: <script type="text/javascript" src="update_cache.js"></script> In <body> section I've inserted onload function: <body onload="checkForUpdate()"> In cache.manifest I've put all files I want to cache. It is important now that it works in my case (Apache) just by updating each time the "version" comment. It is also an option to name files with "?ver=001" or something at the end of name but it's not needed. Changing just # version 1.01 triggers cache update event. CACHE MANIFEST # version 1.01 style.css imgs/logo.png #all other files It's important to include 1., 2. and 3. points only in index.html. Otherwise GET http://foo.bar/resource.ext net::ERR_FAILED occurs because every "child" file tries to cache the page while the page is already cached. In update_cache.js file I've put this code: function checkForUpdate() { if (window.applicationCache != undefined && window.applicationCache != null) { window.applicationCache.addEventListener('updateready', updateApplication); } } function updateApplication(event) { if (window.applicationCache.status != 4) return; window.applicationCache.removeEventListener('updateready', updateApplication); window.applicationCache.swapCache(); window.location.reload(); }

现在你只需更改文件,在manifest中你必须更新版本注释。现在访问index.html页面将更新缓存。

解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。

并非如此。一种方法是在传递内容时发送适当的头文件,以迫使浏览器重新加载:

确保网页没有在所有浏览器上缓存。

如果你在SO上搜索“缓存头”或类似的东西,你会发现ASP。NET特定的示例。

另一种不太清晰但有时是唯一的方法,如果你不能控制服务器端的头,就是给被调用的资源添加一个随机的GET参数:

myimage.gif?random=1923849839

对于静态资源,正确的缓存是使用带有每个部署或文件版本值的查询参数。这将在每次部署后清除缓存。

/Content/css/Site.css?version={FileVersionNumber}

这是ASP。NET MVC的例子。

<link href="@Url.Content("~/Content/Css/Reset.css")?version=@this.GetType().Assembly.GetName().Version" rel="stylesheet" type="text/css" />

不要忘记更新程序集版本。

如果这是关于.css和.js的更改,那么一种方法是通过在每个版本的文件名后面附加类似“_versionNo”的东西来“缓存破坏”。例如:

script_1.0.css // This is the URL for release 1.0
script_1.1.css // This is the URL for release 1.1
script_1.2.css // etc.

或者在文件名后面:

script.css?v=1.0 // This is the URL for release 1.0
script.css?v=1.1 // This is the URL for release 1.1
script.css?v=1.2 // etc.

您可以检查这个链接,看看它是如何工作的。

更新2012

这是一个老问题,但我认为它需要一个更最新的答案,因为现在有一种方法可以更好地控制网站缓存。

在离线Web应用程序(实际上是任何HTML5网站)中,applicationCache.swapCache()可以用来更新网站的缓存版本,而不需要手动重新加载页面。

这是一个来自HTML5 Rocks上使用应用程序缓存的初学者指南的代码示例,解释了如何将用户更新到站点的最新版本:

// Check if a new cache is available on page load.
window.addEventListener('load', function(e) {

  window.applicationCache.addEventListener('updateready', function(e) {
    if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
      // Browser downloaded a new app cache.
      // Swap it in and reload the page to get the new hotness.
      window.applicationCache.swapCache();
      if (confirm('A new version of this site is available. Load it?')) {
        window.location.reload();
      }
    } else {
      // Manifest didn't changed. Nothing new to server.
    }
  }, false);

}, false);

有关更多信息,请参见在Mozilla开发者网络上使用应用程序缓存。

更新2016

网络上的事物变化很快。 这个问题是在2009年提出的,在2012年,我发布了一篇关于处理问题中描述的问题的新方法的更新。又过了4年,现在看来它已经被弃用了。感谢cgaldiolo在评论中指出这一点。

目前,截至2016年7月,HTML标准,第7.9节,脱机Web应用程序包括一个弃用警告:

这个特性正在从Web平台上移除。 (这是一个漫长的过程,需要很多年。)使用任何 此时非常不鼓励使用离线Web应用程序特性。 使用服务工作者代替。

在Mozilla开发者网络上使用应用程序缓存也是如此,我在2012年引用过:

已弃用该特性已从Web标准中移除。 虽然一些浏览器可能仍然支持它,但它仍在开发过程中 被删除。不要在旧的或新的项目中使用它。页面或Web应用程序 使用它可能会随时损坏。

请参见Bug 1204581 -如果启用service worker获取拦截,为AppCache添加弃用通知。