我看了看,但似乎没有给出答案……看来我的 js 文件中的 window.fbAsyncInit 没有被调用。这是一个测试应用程序,让我自己熟悉非常基础的 API,这也是 PHP 代码片段出现在 HTML 部分的原因;)。我的一个想法是 JS 看不到 fb-root DIV,所以我尝试在之后加载 JS,甚至去掉除了 Facebook 的东西之外的所有东西,仍然什么都没有...... info DIV 的 innerHTML 和控制台都没有显示任何东西. 我确实在 DIV 和控制台中收到“SDK 已加载”消息,所以我知道 API 正在加载。我也没有收到任何错误。有没有人知道为什么这不起作用?还尝试了 FF、Chrome、Safari 和 IE(mac 和 pc),结果相同。
代码最初来自一个教程网站(稍作调整:)但我仔细检查了 facebook 文档,它与他所拥有的相同。
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Guess Test</title>
<link href="guess.css" rel="stylesheet" type="text/css">
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="guess.js" type="text/javascript"></script>
</head>
<body>
<div id="fb-root"></div>
<?php echo "Welcome to the guess test program :D<br>"; ?>
<div id="alertbox"></div>
<br>
<div>Enter your guess between 1 and 10: <input type=text id='myGuess' name='myGuess'>
<input type='button' onClick='Guess()' value='Guess'>
<br>
<br>
<input type='button' onClick='document.getElementById("alertbox").innerHTML = "reset"; Restart()' value='Start Again'>
<br>
<div id="info"><div>
<script type=text/javascript>Restart();</script>
</body>
</html>
Javascript(js 文件)...ID 和 URL 已删除 :)
var iRandom;
var cheat=false;
var fb_login = "foo";
fb_login = "Start";
function Restart() {
document.getElementById("info").innerHTML = fb_login;
iRandom = Math.floor(Math.random()*10)+1;
if(cheat)
document.getElementById("info").innerHTML = iRandom;
var tempstring = document.getElementById("alertbox").innerHTML;
if((tempstring == "") || (tempstring == "reset")) {
document.getElementById("alertbox").innerHTML = "OK, I am thinking of a number between 1 and 10";
}
}
function Guess() {
var yourGuess = document.getElementById('myGuess').value;
if (yourGuess>iRandom) {
document.getElementById("alertbox").innerHTML = "Too High.";
}
if (yourGuess<iRandom) {
document.getElementById("alertbox").innerHTML = "Too Low.";
}
if (yourGuess==iRandom) {
document.getElementById("alertbox").innerHTML = "Well done! You guessed it.";
Restart();
}
}
window.fbAsyncInit = function() {
console.log("START FB init");
fb_login = "START FB init";
FB.init({
appId : 'USED-MY-APP-ID', // App ID
channelUrl : '//MYURL.com/facebook/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
frictionlessRequests : true // enable frictionless requests
});
// Additional initialization code here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
fb_login = "Welcome back";
alert("Welcome back");
}
else if (response.status === 'not_authorized') {
//User is logged into Facebook, but not your App
//LOG IN function
fb_login = "App not authorized";
alert("App not authorized");
}
else {
// User is not logged into Facebook at all
window.top.location ='https://www.facebook.com/index.php';
fb_login = "no facebook";
alert("no facebook");
}
});
};
// Load the SDK Asynchronously
(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);
fb_login = "SDK loaded";
console.log("SDK loaded");
}(document));