Handling code which relies on jQuery before jQuery is loaded

前端 未结 10 1003
挽巷
挽巷 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:17

    The best way I have found is to write the code in a function and call the function after jquery is loaded:

    function RunAfterjQ(){
    // Codes that uses jQuery
    }
    
    
    
    

    Update: For master pages, you can define an array to push functions in the head of the master page:

    var afterJQ = [];
    

    then at the bottom of master page run all the functions pushed in to this array:

    
    
    

    Everywhere that you need to use javascript that relies on jQuery and is before jQuery is defined just push it in to this array:

    afterJQ.push( function() { 
        // this code will execute after jQuery is loaded.
     });
    

提交回复
热议问题