0
<script type="text/javascript">
    $.getScript('http://connect.facebook.net/sv_SE/all.js#xfbml=1', function(){
        $('body').append('<div id="fb-root"></div><div class="fb-like" data-href="http://www.google.se" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false" data-font="verdana"></div>');
    });
</script>

No button is showing. Isnt this the correct way to use getScript?

1
  • I think you need to pass app id also. For e.g. http://connect.facebook.net/en_US/all.js#xfbml=1&appId=<<your app id>> Commented Feb 4, 2012 at 10:31

3 Answers 3

1

You should load the DIV into the DOM first, then load the script in to parse the DOM objects.

<script type="text/javascript">
    $('body').append('<div id="fb-root"></div><div class="fb-like" data-href="http://www.google.se" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false" data-font="verdana"></div>');
    $.getScript('http://connect.facebook.net/sv_SE/all.js#xfbml=1', function(){

    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

The DIVs fb-root and fb-like need to be already in the DOM before the FB like button script executes. The script looks for a DIV with id=fb-root and attempts to insert the FB like button inside it. In your case, you are adding the DIV after the script is downloaded and executd. As a result, no button will appear.

It should work if you change your code to something like:

// append fb-root and fb-like DIVs (with display: none) before getting the FB script
// or even better, add them as part of your page itself with display: none
$('body').append('<div id="fb-root" style="display: none"></div><div class="fb-like" data-href="http://www.google.se" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false" data-font="verdana" style="display: none"></div>');

$.getScript('http://connect.facebook.net/sv_SE/all.js#xfbml=1', function(){
    $('#fb-root').show(); // show the DIVs
    $('#fb-like').show();
});

2 Comments

No need to hide the fb-root as it never displays anything anyways from what I've seen.
@CoderFromOuterSpace - +1 Yes, hiding fb-root is not really necessary. Put that there more to demonstrate the idea.
0

From the like button dox You can use javascript code like this to do async loading of the scripts.

<div id="fb-root"></div>
<script>
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/sv_SE/all.js#xfbml=1&appId=113830492072210";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.