我在项目的页脚文件中包含以下代码:
<script>
$(document).ready(function(){
window.fbAsyncInit = function() {
FB.init({
appId : '996999210390006',
xfbml : true,
version : 'v2.6'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
});
function sharefbDialog()
{
alert("fb");
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/',
}, function(response){});
}
</script>
我调用sharefbDialog函数onclick of anchor tag但它总是给fb未定义错误。最初我把fb.init代码放在document.ready之外,但即使这样它也没有用。我已经研究了所有stackoverflow解决方案但没有成功。
答案 0 :(得分:0)
你也可以这样调用这个函数。
<html5>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '996999210390006',
xfbml : true,
version : 'v2.6'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function sharefbDialog()
{
alert("fb");
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/',
}, function(response){});
}
sharefbDialog();
</script>
<head>
<body>
<h2>Hello<h2>
<a href="" onclick="sharefbDialog();" >Testing </a>
</body>
</html5>