我正在嘗試向我創建的窗口小部件添加一個Facebook Like按鈕。我用像按鈕添加Facebook到我的網頁的代碼如下:即使我剛剛使用它,FB也是未定義的
widget.html
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '263071593731910',
status : false, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<div id="fb-button"></div>
<script type="text/javascript" src="widget.js"></script>
widget.js
$(function(){
var fb = $(document.createElement("fb:like"));
fb.attr({
href: data.facebook,
send: false,
layout: 'button_count',
width: 70,
'show_faces': false
});
$("#fb-button").empty().append(fb);
FB.XFBML.parse($("#fb-button").get(0));
FB.Event.subscribe('edge.create',changeView);
});
*的changeView功能確實存在,以及在JavaScript中。
當我運行代碼時,即使創建按鈕,我也會收到錯誤:Uncaught ReferenceError: FB is not defined
。該錯誤指向包含FB.XFBML.parse代碼的行。有什麼我需要在JavaScript中做不同的事情嗎?
嘗試使用window.FB或document.FB –