Handling code which relies on jQuery before jQuery is loaded

前端 未结 10 1008
挽巷
挽巷 2020-12-08 06:31

I\'d like to follow the general guideline of putting all JavaScript at the very bottom of the page, to speed up loading time and also to take care of some pesky issues with

10条回答
  •  春和景丽
    2020-12-08 07:16

    I've wrote simple js code for handle such cases: https://github.com/Yorkii/wait-for

    Then you can use it like this

    waitFor('jQuery', function () {
       //jQuery is loaded
       jQuery('body').addClass('done');
    });
    

    You can even wait for multiple libraries

    waitFor(['jQuery', 'MyAppClass'], function () {
       //Both libs are loaded
    });
    

提交回复
热议问题