load scripts asynchronously

前端 未结 19 1209
粉色の甜心
粉色の甜心 2020-11-22 12:01

I am using several plugins, custom widgets and some other libraries from JQuery. as a result I have several .js and .css files. I need to create a loader for my site because

19条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 12:43

    I would suggest you take a look at Modernizr. Its a small light weight library that you can asynchronously load your javascript with features that allow you to check if the file is loaded and execute the script in the other you specify.

    Here is an example of loading jquery:

    Modernizr.load([
      {
        load: '//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js',
        complete: function () {
          if ( !window.jQuery ) {
                Modernizr.load('js/libs/jquery-1.6.1.min.js');
          }
        }
      },
      {
        // This will wait for the fallback to load and
        // execute if it needs to.
        load: 'needs-jQuery.js'
      }
    ]);
    

提交回复
热议问题