我在我的网络应用程序中使用 JQuery 库和一些自定义的 JavaScript。我将它们组合并压缩成一个 JavaScript 文件,并将其作为静态脚本放在主体末尾,这通常可以获得最佳性能,同意吗?
<script src="../js/Default.js" type="text/javascript"></script>
此外,还有一些 .net ajax 控件,例如<ajax:calendarExtender>
应用于某些页面。一些文章,http://am-blog.no-ip.org/BlogEngine/post/2010/04/12/Increase-AJAX-Performance-by-combining-Scripts-using-Scriptmanager-Composite-Scripts.aspx建议使用 <asp:CompositeScript>
合成这些控件所需的默认 asp.net JavaScripts,例如<ajax:calendarExtender>
因为我的 Web 应用程序只有一个 ScriptManager,它位于 MasterPage 中的 Form 开头。合成和压缩这些 .net JS 以使其使用类似于我的静态脚本的最佳方法是什么?由于这些 ajax 控件并未应用于每个页面,我是否仍应在母版页中组合所有 JavasSript? (猜想每个子页面中的引用代理不会有帮助吗?)
问候,
最佳答案
我认为除了 gZip 之外,您不需要压缩更多此文件。
当然你需要在这里证明你在 webresource 上的 js 文件不是调试版本。
如果您尝试更改此文件的位置,例如尝试将它们放在末尾,那么您可能会遇到 javascript 错误,因为 asp.net 控件可能需要找到此函数
我只建议在客户端添加一些缓存。您可以在应用程序开始请求时执行此操作 - 例如:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
// get the file name
string cTheFile = HttpContext.Current.Request.Path;
// here just see if this is the files that you wish to add some extra cache
if (cTheFile.EndsWith("WebResource.axd", StringComparison.InvariantCultureIgnoreCase))
{
// add some cache - what ever you think, or add an eTag
app.Response.Cache.SetExpires(DateTime.UtcNow.AddHours(4));
app.Response.Cache.SetMaxAge(new TimeSpan(4, 0, 0));
app.Response.Cache.SetCacheability(HttpCacheability.Public);
}
}
关于javascript - 如何合成和压缩 Javascripts (.net ajax) 以获得最佳性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5072077/