如何在“.js”文件中使用facebook发布流方法 [英] How to use facebook publish stream method in a '.js' file
问题描述
我正在使用位于.php文件中的波纹管码,以发布流。
I am using bellow code which is in a '.php' file, to publish stream.
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init(
{
appId : 'MY_APP_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
var publish = {
method: 'stream.publish',
message: '',
picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
link : 'http://apps.facebook.com/puzzlegts/',
name: 'Go to image',
caption: 'taged you in a photo',
description: 'test 2',
actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
};
FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});
</script>
现在我有一个名为'publish.js'的javascript文件,我想添加发布流方法我的'publish.js'文件中的一个函数。任何人都可以告诉我怎么做?
出现问题,因为我不能在'.js'中使用'< script>
'标签文件。因此,我不能包含bellow代码。
Now I have a javascript file named 'publish.js' and I want to add publish stream method to a function in my 'publish.js' file. Can anyone please tell me how to do this?
Problem arise since I can't use '<script>
' tag in a '.js' file.Therefore I can't include bellow code.
<script src="http://connect.facebook.net/en_US/all.js"></script>
或者任何人都可以为.js文件发布适合的代码
Or can anyone please post the suitable code for a '.js' file
推荐答案
您可以以编程方式添加它,或者只是使用异步加载:
You have add it programmatically then or just use the asynchronous loading:
window.fbAsyncInit = function() {
FB.init({
appId : 'MY_APP_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
var publish = {
method: 'stream.publish',
message: '',
picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
link : 'http://apps.facebook.com/puzzlegts/',
name: 'Go to image',
caption: 'taged you in a photo',
description: 'test 2',
actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
};
FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
注意:
- 您需要确保您的标记中有
fb-root
div。 - 这只会在库中点燃
all.js
已完全加载
- You need to insure that you have the
fb-root
div is in your markup - this will only fire once the library
all.js
is fully loaded
这篇关于如何在“.js”文件中使用facebook发布流方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!