1、适当使用缓存机制

2、使用CDN内容分发来访问Jquery脚本:

     (1)自己公司架设CDN服务器

     (2)使用第三方公司的,比如微软,谷歌等公司的CDN,但有时候不太靠谱

3、JS/CSS文件的打包合并(Bundling)及压缩(Minification)

将多个JS或CSS文件打包合并成一个文件,并在网站发布之后进行压缩,从而减少HTTP请求次数,提高网络加载速度和页面解析速度。压缩功能实现了对javascript脚本和CSS进行压缩的功能,它能够去除脚本或样式中不必要的空白和注释,同时能够优化脚本变量名的长度

例如在BundleConfig.cs里面配置捆绑js和css文件:

如何提高Ajax性能
using System.Web;
using System.Web.Optimization;

namespace MvcExample
{
    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
        }
    }
}
如何提高Ajax性能

 记得在Global.asax中注册一下:

BundleConfig.RegisterBundles(BundleTable.Bundles);

页面引用时可以这样引用:

如何提高Ajax性能
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</head>
<body>
    @RenderBody()
</body>
</html>
如何提高Ajax性能

启用JS/CSS文件压缩合并:

  • Web.config中配置
<compilation debug="false" targetFramework="4.0" />
  • 在BundleConfig.cs或Global.asax中添加以下代码即可:
BundleTable.EnableOptimizations = true;

4、最好将js脚本文件放在view页面下面一点

相关文章:

  • 2021-12-12
  • 2021-08-29
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-01-10
  • 2021-12-03
猜你喜欢
  • 2021-12-07
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2018-04-13
相关资源
相似解决方案