如何在css文件中动态添加版本
how to dynamically add the version in the css file?
如何使用编译ASP或mb javascript在css文件(例如:main.css?v=1231294012278)中动态添加版本。这解决了缓存的问题
我曾经为此使用ScriptManager
。我检测到ModifiedDateTime
并将其添加到文件中,因此无法缓存
这是完整的代码
<asp:ScriptManager runat="server" EnablePageMethods="true" EnablePartialRendering="true"
ID="sm" OnResolveScriptReference="sm_ResolveScriptReference">
<Scripts>
<asp:ScriptReference Path="~/scripts/jquery-1.4.2.min.js" />
</Scripts>
</asp:ScriptManager>
然后
protected void sm_ResolveScriptReference(object sender, ScriptReferenceEventArgs e)
{
if (!String.IsNullOrEmpty(e.Script.Path))
{
e.AddVersionToScriptPath(Server.MapPath(e.Script.Path));
}
}
public static void AddVersionToScriptPath(this System.Web.UI.ScriptReferenceEventArgs scrArg, string fullpath)
{
string scriptpath = scrArg.Script.Path;
var fn = GetFileWriteTime(fullpath);
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["minifyJS"]) && ConfigurationManager.AppSettings["minifyJS"] == "1")
scriptpath = scriptpath.Contains(".min.") ? scriptpath : scriptpath.Replace(".js", ".min.js");
scrArg.Script.Path = String.Format("{0}?{1}", scriptpath, fn);
}
public static string GetFileWriteTime(string fullpath)
{
string fw = "";
if (File.Exists(fullpath))
{
FileInfo fi = new FileInfo(fullpath);
fw += fi.LastWriteTime.ToString("yyMMddhhmm");
}
return fw;
}
试试这个:
<link rel="stylesheet" type="text/css" href="http://mysite/style.css?id="+generateUUID()>
GenrateUUID
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
});
return uuid;
};
相关文章:
- 如何在Google柱状图中动态添加行/列
- 使用jQuery动态添加表并在其中动态添加行
- 如何使用Angular动态添加iframe-src
- 如何向JSON数组动态添加属性
- 动态添加通过json创建的选择项
- 使用jQuery根据动态生成的html动态添加html
- 使用.on动态添加jquery/js不知道的html元素
- JS动态添加字段-删除按钮不起作用
- 动态添加AngularJS脚本
- 将表行动态添加到表或tbody元素中
- jQuery-通过AJAX调用动态添加具有值的表单元格
- 将特定属性动态添加到元素中
- 在highcharts.js中向点弹出窗口动态添加文本
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- 使用jQuery动态添加onClick事件
- 如何在Opencart中动态添加JavaScript
- 剑道UI下载列表:如何动态添加新元素
- 动态添加复选框和输入框 jQuery
- 动态添加哈希标记;t工作
- 将图表动态添加到组合高图表中