javascript - 如何合成和压缩 Javascripts (.net ajax) 以获得最佳性能

标签 javascript jquery asp.net ajax asp.net-ajax

我在我的网络应用程序中使用 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/

相关文章:

php - ajax 表单请求不适用于 mod_rewrite

asp.net - Parent.FindControl() 不工作?

c# - 将文本框组合在一起,以便可以通过简单的循环更改它们的所有属性

javascript - 如何在 JQuery 中为 addClass/removeclass 设置动画

javascript - html Javascript 通过 onmouseover 在 <a> 按钮上 move 图像

jQuery watch div

javascript - 基于另一个下拉列表启用/禁用下拉列表

c# - .Net framework 3.0、3.5、2.0 在 Visual Studio 2012 中不显示,在 Windows 8.1 中仅显示 4.0 和 4.5

javascript - 使用 iframe 通过 Chrome 扩展将本地 HTML 文件注入(inject)网页?

javascript - 我如何使用 Twitter Bootstrap 中的弹出框来显示复选框、div block 、 radio 、图像等?