1
我想初始化facebook javascript sdk並在用戶點擊index.html頁面上的鏈接後檢查登錄狀態。由於某種原因,這不會工作。 jQuery正在工作,當我單獨測試init代碼時,它可以正常工作,但當我將它放在一起時,它停止工作。任何想法爲什麼?如何使用jquery事件觸發window.fbAsyncInit
這是我的index.html頁面:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<div id="fb-root"></div>
<script>
//這個jQuery是應該觸發init進程
$('document').ready(function(){
$('a').click(function(){
alert("this works");
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : '39672*******', // App ID
channelUrl : '//http://spilot.koding.com/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// get login status
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected: redirect to start scene page
alert("testing");
} else if (response.status === 'not_authorized') {
// not_authorized
login();
} else {
// not_logged_in
login();
}
});
};
// 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);
}(document));
//顯示登錄框
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
alert("connected");
} else {
// cancelled
}
});
}
});
});
</script>
<div id="findScene"><a href=""><h1>Find</h1></a></div>
<div id="startScene"><a href=""><h1>Host</h1></a></div>
</body>
</html>
我試着將javascript:window.fbAsyncInit()分配給其中一個鏈接中的href,但它不起作用。這是你在$(document).ready函數之外賦值處理函數的意思嗎? – Spilot 2013-03-07 03:15:48
爲什麼要在點擊鏈接時運行此代碼?不,您希望它儘快運行 - 以便在您想要使用SDK時準備好SDK。只需將其放置在現成的處理程序的外部,就在(現有的)