如何关闭通过FB.ui()打开的Facebook SDK对话框? [英] How to close a facebook SDK dialog opened with FB.ui()?
问题描述
我成功显示了一个邀请朋友对话框(如下所示的代码).当用户单击跳过时,iframe/对话框会显示一个新页面.但是从这一点上,我无法找到一种方法来关闭iframe/对话框. FB.ui不返回任何对象,似乎没有Javascript SDK方法,使用DOM进行遍历和操作对于任何FB代码更改都是脆弱的.
I'm successfully displaying an invite friend dialog (code shown below). When user clicks skip the iframe/dialog shows a new page. However from this point I can't find a way how to close the iframe/dialog. FB.ui doesn't return any object, there doesn't seem to be a Javascript SDK method and traversing and manipulating with the DOM will be brittle to any FB code changes.
有什么想法吗?
function popupInviteForm(actionUrl) {
var fbmlString = '<fb:fbml>' +
' <fb:request-form type="POST" content="Play against me in game?" action="' + actionUrl + '" method="post" >' +
' <fb:multi-friend-selector target="_self" exclude_ids="" max="20" cols="4" rows="3" showborder="false" actiontext="Invite friends!" />' +
' </fb:request-form>' +
'</fb:fbml>';
FB.ui({
method: 'fbml.dialog',
fbml: fbmlString,
display: 'dialog',
size: {width:640,height:480}, width:640, height:480
});
$(".FB_UI_Dialog").css('width', $(window).width()*0.8);
}
(注意:我已经在 facebook论坛上发布了相同的问题没有任何回应.如果其中任何一个都有答案,我都会同时更新两者.
(Note: I have posted the same question on the facebook forum with no response. I will update both, should there be an answer on either.)
The Javascript code was adapted from a stack overflow answer.
推荐答案
我也遇到了同样的麻烦.第二天寻找解决方案.并找到了一种方法: 要关闭活动的FB对话框,您应该在可用的FB JS以及调用FB.ui的父窗口中执行以下代码:
I have same trouble. Second day looking for a solution. And just found one way: for closing active FB dialog you should perform followed code in parent window where FB JS is available and where FB.ui was called:
FB.Dialog.remove(FB.Dialog._active);
因此,如果您希望邀请对话框自动关闭并且不重定向到任何其他页面,请使用以下步骤:
So, if you want your invite dialog auto closes and don't redirects to any other page, use these steps:
1)将和的目标属性设置为"_self":
1) Set target attr of and as "_self":
target ="_ self"
target="_self"
2)在您的网站上创建一些回调URL/页面,例如
https://mysite/close_dialog.html
2) create some callback url/page on your site, for example
https://mysite/close_dialog.html
3)将操作属性attr设置为刚创建的网址:
3) Set action attr of as just created url:
action ="http://mysite/close_dialog.html"
action="http://mysite/close_dialog.html"
4)在您的close_dialog.html中,紧跟着JS:
4) In your close_dialog.html put followed JS:
<script type="text/javascript">
if (document.location.protocol == 'https:') {
document.location = 'http://mysite/close_dialog.html';
} else {
top.window.FB.Dialog.remove(top.window.FB.Dialog._active);
};
</script>
更新:
5)这种方式还有一个问题: FB iframe由https加载,但如果请求形式的操作"属性使用"http",则用户将收到浏览器警告.但是,如果请求形式的操作"具有"https",则iframe js无法访问由"http"加载的父级. 因此,这就是您应使用"https"操作的原因
5) There is one issue else in this way: FB iframe loaded by https, but if request-form 'action' attr uses 'http' - user will get browser warning. But if request-form 'action' has 'https' - iframe js cant access to parent loaded by 'http'. So, its the reason why you should use action by 'https'
希望这会有所帮助
如果您有更好的解决方案,或者可以通过这种方式进行改进-请让所有人都知道这一点, 感谢您的任何评论
If you has better solution or you can improve this way - please let all know this, Thanks for any comments
这篇关于如何关闭通过FB.ui()打开的Facebook SDK对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!