简体   繁体   中英

How can I debug a missing Facebook Like Button?

I'm trying to attach the Like Button and Tweet Button to a page using jQuery. The Tweet Button works just fine, but the Like Button won't appear. I don't know what's wrong, but worse yet, I don't get any errors or messages indicating a problem.

How can I get Facebook, Firebug, or anything to tell me what's missing, or throw appropriate errors?

Here is a demonstration on jsfiddle .

Here is the full code for those of you who don't want to go to jsfiddle:

/**
 * Generate the container elements for social media stuff.
 */
$(function generateSocMedElements($) {
    var id = "socmed",
        $container,
        $fb_root,
        $fb_like,
        $tweet_button;
    if ($("#" + id).length <= 0) {
        $container = $(document.createElement("aside")).
            addClass("socmed").
            attr("id", id);
        $fb_root = $(document.createElement("div")).attr("id", "fb-root");
        $fb_like = $(document.createElement("div")).
            addClass("fb-like").
            attr("data-href", "example.org").
            attr("data-send", "false").
            attr("data-layout", "button_count").
            attr("data-width", 82).attr("data-show-faces", "false");
        $tweet_button = $(document.createElement("a")).
            addClass("twitter-share-button").
            attr("href", "//twitter.com/share").
            attr("data-count", "none").
            text("Tweet");
        $container.
            appendTo($("header").first()).
            append($fb_root).
            append($fb_like).
            append($tweet_button);
    }
});

/**
 * Load the script for the Facebook API.
 */
$(function loadFacebook($) {
    var id = "facebook-jssdk";
    if ($("#" + id).length <= 0) {
        $(document.createElement("script")).
            attr("id", id).
            attr("src", "//connect.facebook.net/en_US/all.js").
            appendTo("head");
    }
});

/**
 * Load the script for the Twitter API.
 */
$(function loadTwitter($) {
    var id = 'twitterSdk';
    if ($("#" + id).length <= 0) {
        $(document.createElement("script")).
            attr("id", id).
            attr("src", "//platform.twitter.com/widgets.js").
            appendTo("head");
    }
});

http://jsfiddle.net/ShawnsSpace/btN89/3/ i was able to get it to work by

  1. adding div tag in header
  2. wrapping generateSocMedElements() in a function and calling after page load.
  3. used facebooks html5 script.

. You will have to tweek this to get the desired look.

<header><h1>OH HAI I HAZ A HEADER</h1>
<div id="fb-root"></div>
</header>

/**
 * Load the script for the Facebook API.
 */

(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/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


/**
 * Load the script for the Twitter API.
 */
$(function loadTwitter($) {
    var id = 'twitterSdk';
    if ($("#" + id).length <= 0) {
        $(document.createElement("script")).
            attr("id", id).
            attr("src", "//platform.twitter.com/widgets.js").
            appendTo("head");
    }
});
function loadthis($){
$(function generateSocMedElements($) {
    var id = "socmed",
        $container,
        $fb_root,
        $fb_like,
        $tweet_button;
    if ($("#" + id).length <= 0) {
        $container = $(document.createElement("aside")).
            addClass("socmed").
            attr("id", id);
        $fb_root = $(document.createElement("div")).attr("id", "fb-root");
        $fb_like = $(document.createElement("div")).
            addClass("fb-like").
            attr("data-href", "example.org").
            attr("data-send", "false").
            attr("data-layout", "button_count").
            attr("data-width", 82).attr("data-show-faces", "false");
        $tweet_button = $(document.createElement("a")).
            addClass("twitter-share-button").
            attr("href", "//twitter.com/share").
            attr("data-count", "none").
            text("Tweet");
        $container.
            appendTo($("header").first()).
            append($fb_root).
            append($fb_like).
            append($tweet_button);
    }
});
}
loadthis($);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM