如何加载最新的JavaScript文件的每个用户访问该网站时, [英] how to load the latest javascript files every time the user visits the website

查看:83
本文介绍了如何加载最新的JavaScript文件的每个用户访问该网站时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/8230589/how-do-i-force-the-refresh-of-javascript-files-in-a-browser\">How我强制javascript文件的刷新在浏览器?

我的ASP.NET MVC应用程序的基础和JavaScript文件包含在.csHtml文件。

My application in ASP.NET MVC based and javascript files are included in .csHtml file.

我需要这个,这样用户不必做[Ctrl + F5键】或手动清除缓存和JavaScript文件加载到浏览器每次最新版本。

I require this so that the user do not have to do a [Ctrl+F5] or manually clear cache and the most recent version of javascript file is loaded everytime in the browser.

我AP preciate。

I appreciate if some examples can be provided.

主技术是使用一个虚设paramater而包括文件。
此外,我不什么手动每次我修改js文件时更改参数。需要一些例子,如果这可以自动完成。

Primary technique suggested is to use a dummy paramater while including the file. Also I do not what to change the parameter manually every time I modify a js file. Need some examples if this can be done automatically.

编辑1:
请提供解决方案,这与ASP.NET MVC预期。

EDIT 1: Please provide solution to this with ASP.NET MVC prospective.

推荐答案

您想要使用的捆绑和缩小。根据您的MVC的版本,实现略有不同。在最新的版本中,默认情况下使用。

You want to use Bundling and Minification. Depending on your version of MVC, the implementation is slightly different. In the newest version, it is used by default.

捆绑和微小将结合所有脚本(和样式表),再压缩成一个文件(或多个,这取决于你如何使用它),并为他们服务了一个独特的参数。任何时间在该特定束的文件改变(以及因此的用户将需要下载新的文件)参数自动变为

Bundling and Minification will combine and minify all your scripts (and styles sheets) into one file (or multiple, depending on how you use it) and serve them up with a unique parameter. Any time a file changes in that particular bundle (and thus the user would require to download the new files) the parameter automatically changes.

有关MVC3,您需要安装的Microsoft Web优化

For MVC3, you'll need to install Microsoft Web Optimization.

然后在你的global.ascx,你会做这样的事情,并从的Application_Start 调用它:

Then in your global.ascx, you'd do something like this and call it from Application_Start:

private static void SetupBundling()
{
     var jsBundle = new Bundle("~/Scripts/js", typeof(JsMinify));
     jsBundle.AddDirectory("~/Scripts/", "*.js", false);
     jsBundle.AddDirectory("~/Scripts/anothr-good-folder/", "*.js", false);
     BundleTable.Bundles.Add(jsBundle);

     var randomBundle = new Bundle("~/Scripts/random", typeof(JsMinify));
     randomBundle.AddFile("~/Scripts/random/main.js");
     randomBundle.AddFile("~/Scripts/random/cool.js");
     BundleTable.Bundles.Add(randomBundle);

     var cssBundle = new Bundle("~/Content/css", typeof(CssMinify));
     cssBundle.AddDirectory("~/Content/", "*.css", false);
     BundleTable.Bundles.Add(cssBundle);
}

所以,首先将一束束在每个的.js 文件〜/脚本文件夹中。在你的头文件,你可以参考它,如:

So that first bundle will bundle every .js file in your ~/Scripts folder. In your head file you can reference it like:

<script src="@Microsoft.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")" type="text/javascript"></script>

和它被渲染,如:

<script src="/Scripts/js?v=-2573834892993289" type="text/javascript"></script>

和任何时候你的的.js 文件更改(或的.css )之一,所以将参数。

And any time one of your .js files change (or .css), so will the parameter.

执行类似的CSS捆绑,并且,如果你想引用 randomBundle 仅在某些页面。

Similar implementation for the CSS bundle, and also if you want to reference the randomBundle only on certain pages.

这篇关于如何加载最新的JavaScript文件的每个用户访问该网站时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆