我看了但是答案似乎没有出现......似乎我的js文件中的window.fbAsyncInit没有被调用。这是一个测试应用程序,以熟悉API非常基本,这就是为什么PHP snippit在HTML部分;)。我有一个想法是JS无法看到fb-root DIV所以我尝试加载JS之后甚至剥离了除了Facebook之外的所有东西,但仍然没有...信息DIV的innerHTML和控制台都没有显示任何内容。我在DIV和控制台中收到“SDK loaded”消息,因此我知道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 file)...删除了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));