load jquery after the page is fully loaded

前端 未结 11 1412
甜味超标
甜味超标 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:56

    It is advised to load your scripts at the bottom of your <body> block to speed up the page load, like this:

    <body>
    <!-- your content -->
    <!-- your scripts -->
    <script src=".."></script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-31 08:57

    You can also use:

    $(window).bind("load", function() { 
        // Your code here.
    });
    
    0 讨论(0)
  • 2020-12-31 08:57

    Include your scripts at the bottom of the page before closing body tag.

    More info HERE.

    0 讨论(0)
  • 2020-12-31 08:58

    if you can load jQuery from your own server, then you can append this to your jQuery file:

    jQuery(document).trigger('jquery.loaded');

    then you can bind to that triggered event.

    0 讨论(0)
  • 2020-12-31 08:59

    You can try using your function and using a timeout waiting until the jQuery object is loaded

    Code:

    document.onload=function(){
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", 'http://code.jquery.com/jquery-1.7.2.min.js');
        document.getElementsByTagName("head")[0].appendChild(fileref);
        waitForjQuery();
    }
    
    function waitForjQuery() {
        if (typeof jQuery != 'undefined') {
            // do some stuff
        } else {
            window.setTimeout(function () { waitForjQuery(); }, 100);
        }
    }
    
    0 讨论(0)
提交回复
热议问题