3

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

<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>

The problem is that it's blocking the page load at that part, how can I insert this tag after page load and still have the script execute? I'll like to do it in an unobtrusive way, ideas?

JP Silvashy
  • 46,977
  • 48
  • 149
  • 227

5 Answers5

9

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.");
    });
});
ChessWhiz
  • 4,656
  • 3
  • 26
  • 40
3

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

Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
3

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);
 });
buley
  • 28,032
  • 17
  • 85
  • 106
0

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

Community
  • 1
  • 1
Axel Fontaine
  • 34,542
  • 16
  • 106
  • 137
0

you could do something like:

$(function() {
  $.getScript('http://static.ak.fbcdn.net/connect.php/js/FB.Share', function() {
    //do nothing here
  });
});
Victor
  • 4,721
  • 1
  • 26
  • 31