function not defined 错误如何解决

How to resolve the error function not defined

我正在尝试 post Facebook 墙上的图片我正在使用以下代码:

<script type="text/javascript">
window.fbAsyncInit = function() {
    FB.init({
    appId      : 'XXXXXXXX', // replace your app id here
    channelUrl : 'http://localhost/channel.html', 
    status     : true, 
    cookie     : true, 
    xfbml      : true  
    });
};
(function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));

function FBLogin(){
    FB.login(function(response){
        if(response.authResponse){
            window.location.reload();
        }
    }, {scope: 'email'});
}
function post_on_wall()
{
            var share_name          = 'rohitashav';
            var share_link          = 'http://rohitashavsinghal.com';
            var share_image         = 'http://rohitashavsinghal.com/fb/love.jpg';
            var share_caption       = 'testing';
            var share_description   = 'test desc';

            var share_redirect      = 'http://rohitashavsinghal.com/';


            FB.ui(
            {
                method:         'feed',
                name:           share_name,
                link:           share_link,
                picture:        share_image,
                caption:        share_caption,
                description:    share_description
                //message:      ''
            },
            function(response) 
            {
                if (response && response.post_id) 
                {
                    alert('thanks for sharing!');
                    window.top.location.href = share_redirect;
                } 
                else 
                {
                    alert('Post was not published.');
                }
            }
}
</script>

并按如下方式创建按钮:

<input type="button" value="Post on Wall" onClick="post_on_wall();" />

当我点击这个按钮时 console 我收到以下错误:

post_on_wall is not defined 而我已经定义了这个函数

为什么会出现此错误?

您的 FB.ui 通话未结束。您需要在回调函数后添加);。否则 post_on_wall 函数无效。老实说,我真的很惊讶你没有在控制台中收到有关该错误的信息。

此外,上面您对包含 Facebook JS 的调用放错了地方 )。您需要将 }(document)); 更改为 })(document);

我注意到您的代码中存在一些错误。

1.

 (function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
  }(document));`

如果我的 Javascript 正确,应该是这样:

(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
})(document);
  1. 在您的 FB.UI 调用中,您有几个错误:

    FB.ui( //method opened
        {
            method:         'feed',
            name:           share_name,
            link:           share_link,
            picture:        share_image,
            caption:        share_caption,
            description:    share_description
            //message:      ''
        },
        function(response) 
        {
            if (response && response.post_id) 
            {
                alert('thanks for sharing!');
                window.top.location.href = share_redirect;
            } 
            else 
            {
                alert('Post was not published.');
            }
    

现在,打开的方法调用没有匹配的右括号 )