zoukankan      html  css  js  c++  java
  • yepnope.js – 异步加载资源文件

    yepnope.js是一个能够根据输入条件来选择性异步加载资源文件的js脚本,可以在页面上仅加载用户需要的js/css。

    典型代码示例

    yepnope({
    test : Modernizr.geolocation,
    yep : 'normal.js',
    nope : ['polyfill.js', 'wrapper.js']
    });


    当Modernizr.geolocation为真时,加载yep项也就是”normal.js”,否则加载nope项——可以同时加载多个文件。

    yepnope和现有的xxx script loader有什么区别?

    个人认为主要 是这两点:

    • 可以同时处理javascript以及css
    • 能够按条件加载

    yepnope的全部参数

    yepnope([{
    test : /* boolean(ish) - 你要检查真伪的表达式 */,
    yep : /* array (of strings) | string - test为true时加载这项 */,
    nope : /* array (of strings) | string - test为false时加载这项 */,
    both : /* array (of strings) | string - 什么情况下都加载 */,
    load : /* array (of strings) | string - 什么情况下都加载 */,
    callback : /* function ( testResult, key ) | object { key : fn } 当某个url加载成功时执行相应的方法 */,
    complete : /* function 都加载完成了执行这个方法 */
    }, ... ]);

    这里的参数都可以是array或者object,在加载多个资源文件的时候有用。

    yepnope加载jquery的实例

    yepnope([{
    load: 'http:/­/ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
    complete: function () {
    if (!window.jQuery) {
    yepnope('local/jquery.min.js');
    }
    }
    }, {
    load: 'jquery.plugin.js',
    complete: function () {
    jQuery(function () {
    jQuery('div').plugin();
    });
    }
    }]);

    这段代码异步加载了jquery和jquery.plugin.js,甚至还对jquery加载失败的情况做了一个备用处理。

    原文链接:http://www.ooso.net/archives/591

  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/wmlunge/p/2299254.html
Copyright © 2011-2022 走看看