load jquery after the page is fully loaded

前端 未结 11 1419
甜味超标
甜味超标 2020-12-31 08:32

I\'m looking for a way to load jquery after the page is fully loaded.
well there are lots of questions and answers about it in here, but all describe how to run a scri

11条回答
  •  一整个雨季
    2020-12-31 08:38

    I have asked this question more than 6 years ago, and any answers I got had some flaws. Later I myself worked out a solution that I have been using for years since then. Now that I came across my own question again and I saw that it has many views, I'd like to share it because I think it may help others.

    This problem mainly occurs on Master-Detail type of pages (can be old .master and .aspx pages) or (layout and views in asp.net) or any similar situation maybe on other web development languages, however always there is a master-detail pattern involved.

    For the solution, I just add an array at the beginning of my page:

    
    

    any function that requires jQuery or any other script that would run after this section, instead of running it, I just push it to this array:

    after.push(function(){
         // code that requires scripts that will load later,
         //might be for example a jQuery selector or ...
    }); 
    

    and then at the very end of the page, right before closing the body tag (of course scripts are loaded by now) I run all the functions inside the (here named) after array:

    
    
    

    I find this way very easy, simple and flawless.

提交回复
热议问题