在Facebook API脚本中简化IF / ELSE

时间:2012-10-25 13:59:13

标签: javascript facebook if-statement opengraph facebook-opengraph

我担心这个问题可能有一个简单的答案,但男孩让我努力了 徒劳地找到它!任何帮助,将不胜感激。我们有Facebook应用程序 所有设置都很好,我们的动作和对象使用一点Java工作得很好 脚本。但是......我想通过摆脱来简化我们网站的整个流程 触发操作的Javascript中的Alert对话框。

这就是我们现在所拥有的(有效):

<script type="text/javascript">
function postResonate_with_cambodia()
{
    FB.api('/me/onemandala:resonate_with' +
            '?intention=http://1mandala.org/1action-002','post ',
            function(response) {
            if (!response || response.error) {
            if (confirm('You are not yet signed up for 1Mandala. 1Mandala uses Facebook Connect to     showcase the amazing 1Actions folks are taking on our platform. We will redirect you now to the signup page...')) { window.location.href='http://www.1mandala.org/app-signup ' } else { void('') };;
            } else {
            if (confirm('You are resonating with the intention for 1SewingKit Cambodian Orphanage Empowerment. We will take you now to the project page to take action...')) {  window.location.href='http://1mandala.org/1action-002 ' } else { void('') };;
            }
            });
}
</script>

对话应该非常清楚它应该如何工作。 但是......如果不是那些对话,那么脚本也不会好得多 或者将访问者直接发送到Facebook注册对话,或者发送给 登陆页面?这是我的尝试不起作用。 :-(任何建议都会 非常感谢。

<script type="text/javascript">
function postResonate_with_cambodia()
{
FB.api('/me/onemandala:resonate_with' +
'?intention=http://1mandala.org/1action-002','post',
function(response) {
if (!response || response.error)
{window.location.href='http://www.sign-up-page.com' } ;
} else {
{ window.location.href='http://1mandala.org/app-1action-002' } ;
}
});
}
</script>

1 个答案:

答案 0 :(得分:1)

这很简单,您的语法错误。这是ReferenceError: postResonate_with_cambodia is not defined的原因,但语法问题也应该被报道。

你有太多的闭合支撑(或者开口太少);你也不需要把你的身体包裹在两个街区里。一个就足够了,对于一个单行程你甚至不需要它。校正:

function postResonate_with_cambodia() {
    var url = '/me/onemandala:resonate_with?intention=http://1mandala.org/1action-002';
    FB.api(url, 'post', function(response) {
        if (!response || response.error) {
            window.location.href='http://www.sign-up-page.com';
        } else {
            window.location.href='http://1mandala.org/app-1action-002';
        }
    });
}

始终正确缩进代码。并且在单个语句之后使用分号,而不是在块之后。