首页 > 解决方案 > FB Share JavaScript“未定义share_button”

问题描述

您好,我正在尝试在我的 javascript 代码中实现 facebook 的共享,但是我收到此错误,我尝试了几种建议的解决方案,但没有一个在我的实现中成功。

有人可以帮我纠正这个错误吗?

提前致谢。欢迎任何帮助。


完整代码: https ://pastebin.com/kSgFDf0L


控制台错误。

FB Share JavaScript "share_button is not defined"

$(document).ready(function() {



window.fbAsyncInit = function() {
FB.init({appId: 'xxxx', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
$(document).ready(function(){
$('#share_button').click(function(e){
e.preventDefault();
FB.ui(
{
method: 'feed',
name: 'This is the content of the "name" field.',
link: 'http://www.groupstudy.in/articlePost.php?id=A_111213073144',
picture: 'http://www.groupstudy.in/img/logo3.jpeg',
caption: 'Top 3 reasons why you should care about your finance',
description: "What happens when you don't take care of your finances? Just look at our country -- you spend irresponsibly, get in debt up to your eyeballs, and stress about how you're going to make ends meet. The difference is that you don't have a glut of taxpayers…",
message: ""
});
});
});
    $(".result").on("click", function() {
        
        var id = $(this).attr("data-linkId");
        var url = $(this).attr("href");

        if(!id) {
            alert("data-linkId attribute not found");
        }

        increaseLinkClicks(id, url);

        return false;
    });


    var grid = $(".imageResults");

    grid.on("layoutComplete", function() {
        $(".gridItem img").css("visibility", "visible");
    });

    grid.masonry({
        itemSelector: ".gridItem",
        columnWidth: 200,
        gutter: 5,
        isInitLayout: false
    });


    $("[data-fancybox]").fancybox({

        caption : function( instance, item ) {
            var caption = $(this).data('caption') || '';
            var siteUrl = $(this).data('siteurl') || '';
            


            if ( item.type === 'image' ) {
                caption = (caption.length ? caption + '<br />' : '')
                 + '<a href="' + item.src + '">View image</a><br>'
                 + '<a href="' + siteUrl + '">Visit page</a><br>'
                 + '<a href="' + share_button + '">Share</a>';

            }

标签: javascriptfacebook-javascript-sdk

解决方案


我相信通过声明变量就可以执行函数。我只是不知道它是否会按照您的期望工作。好。我相信这行得通->

caption : function( instance, item ) {
            var caption = $(this).data('caption') || '';
            var siteUrl = $(this).data('siteurl') || '';
            var share_button = $('#share_button') || '';

推荐阅读