JavaScript SDK Facebook:写入文档

JavaScript SDK Facebook: Writing to The Document

本文关键字:文档 SDK Facebook JavaScript      更新时间:2023-09-26

还是初学者,请耐心等待。

我来自PHP,在那里我只能echo <tag>$variable</tag>,但我不太清楚如何真正制作一个用javascript做东西和显示东西的网页。下面,当用户登录时,我尝试了document.write("HI")……它什么都没做(但我知道代码被处理了,因为我可以在控制台中看到它。

很明显,我在javascript方面缺少一个基础知识,我无法完全理解。我试着读了一遍,但我有点迷路了。

<html>
<body>
<h1>My First Web Page</h1>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
            appId      : 'xxx', // App ID
            channelUrl : 'xxx', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
    });
    // Additional initialization code here
    FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                // the user is logged in and connected to your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;
                FB.api('/me', function(response) {
                        console.log('Good to see you, ' + response.name + '.');
                        document.write("HI!");
                });
            } else if (response.status === 'not_authorized') {
                FB.login(function(response) {
                        if (response.authResponse) {
                            console.log('Welcome!  Fetching your information.... ');
                            FB.api('/me', function(response) {
                                    console.log('Good to see you, ' + response.name + '.');
                                    FB.logout(function(response) {
                                            console.log('Logged out.');
                                    });
                            });
                        } else {
                            console.log('User cancelled login or did not fully authorize.');
                        }
 }, {scope: 'email'});
            } else {
                FB.login(function(response) {
                        if (response.authResponse) {
                            console.log('Welcome!  Fetching your information.... ');
                            FB.api('/me', function(response) {
                                    console.log('Good to see you, ' + response.name + '.');
                                    FB.logout(function(response) {
                                            console.log('Logged out.');
                                    });
                            });
                        } else {
                            console.log('User cancelled login or did not fully authorize.');
                        }
 }, {scope: 'email'});
            }
    })
};
// Load the SDK Asynchronously
(function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
}(document));

</script>
</body>
        } else {
        }

你在决赛中什么都没有。对于这种情况,您应该调用FB.login()。