• 如何提高Ajax性能


    1、适当使用缓存机制

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

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

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

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

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

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

    复制代码
    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"));
            }
        }
    }
    复制代码

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

    BundleConfig.RegisterBundles(BundleTable.Bundles);

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

    复制代码
    <!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>
    复制代码

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

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

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

  • 相关阅读:
    STM32F407Discovery开发板使用环境搭建
    NIO初识
    Mac下Boost环境搭建
    Android Studio增加NDK代码编译支持--Mac环境
    LNMP平台搭建---PHP安装篇
    LNMP平台搭建---MySQL安装篇
    支付系统流程
    从html字符串中获取div内容---jquery
    记一次进入新公司快速融入开发团队经历
    DataTable复制自身行
  • 原文地址:https://www.cnblogs.com/tinya/p/4505318.html
Copyright © 2020-2023  润新知