Loading an external script after page load with jQuery

后端 未结 5 453
别跟我提以往
别跟我提以往 2021-01-14 16:27

I\'m a little confused how to do this, basically I have a page that has a Facebook Share button inserted via JavaScript:



        
相关标签:
5条回答
  • 2021-01-14 17:05

    Inject the script tag into the dom as a callback of the document ready event.

    0 讨论(0)
  • 2021-01-14 17:06

    Try something like this:

    $(document).ready(function() {
        var script = document.createElement( 'script' );
        script.src = "http://static.ak.fbcdn.net/connect.php/js/FB.Share";
        script.onload=optionallyDoSomethingWithTheScriptYouJustLoaded();//not needed
        var headID = document.getElementsByTagName("head")[0];         
        headID.appendChild(script);
     });
    
    0 讨论(0)
  • 2021-01-14 17:13

    Use the jQuery getScript command inside $(document).ready. This will download the script after the page has loaded. Example:

    $(document).ready(function() {
        $.getScript("http://static.ak.fbcdn.net/connect.php/js/FB.Share", function() {
            alert("Script loaded and executed.");
        });
    });
    
    0 讨论(0)
  • 2021-01-14 17:17

    you could do something like:

    $(function() {
      $.getScript('http://static.ak.fbcdn.net/connect.php/js/FB.Share', function() {
        //do nothing here
      });
    });
    
    0 讨论(0)
  • 2021-01-14 17:18

    You could use the jquery.getScripts to load it asynchronously.

    0 讨论(0)
提交回复
热议问题