javascript - G+登录按钮回调绑定(bind)两次

标签 javascript google-plus

我在使用 G+ 登录我的网站时发现了这个问题。

我的网站上有工具栏(用 Javascript 呈现)和 G+ 登录按钮,所以我在工具栏文件中附加了 G+ Javascript API

[工具栏-notlogin.php]

<script>
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js?onload=render';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

function render () 
{
        var config = {
            "callback": "loginCallback",
            "clientid": "xxxxxxxxx.apps.googleusercontent.com",
            "cookiepolicy": "single_host_origin",
            "requestvisibleactions": "http://schemas.google.com/AddActivity",
            "scope": "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email"
            };

            $('#googleCustomLoginBtn .buttonText').text('Login with Google');
            gapi.signin.render ("gplusLoginBtn", config);
        }
</script>

当工具栏(使用 G+ JS API)加载时,它会调用回调函数 render() 来呈现 G+ 登录按钮。它工作得很好,所以我的工具栏变成了登录状态(显示用户的个人资料)并且 G+ 登录按钮消失了(HTML 被替换)。

当用户注销时,工具栏将状态更改为未登录(再次替换 HTML),它会再次调用 render() 并且登录按钮起作用。

但问题是当我这次登录时,回调函数 ( loginCallback() ) 调用了两次。好像回调函数又绑定(bind)了。 (我尝试第二次不调用 render() 但登录按钮不起作用。)

有什么办法可以解决吗?谢谢。

最佳答案

有点新手,但我记得在某处读到过,在某些情况下,您可能会不小心多次调用该函数,因为您已将它绑定(bind)到多个元素。您是否尝试过在声明之前直接删除事件处理程序?

这样,如果已经有它的一个实例,它将在添加另一个实例之前被删除。

类似这样的东西

$('#randomID').unbind('submit').bind('submit');

关于javascript - G+登录按钮回调绑定(bind)两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18972853/

相关文章:

javascript - 从 AngularJS 中的本地数组加载更多数据

javascript - DataTables - 使用单个下拉列表在多列中搜索

android - 无法解析符号 PlusShare

search - 如何将网站链接到 google+,以便个人资料图片出现在搜索中

javascript - 我们应该在哪里存储 JavaScript 调用的服务连接 URL?

javascript - Bootstrap 警报仅在 JSFiddle 中显示一次

javascript - 一个奇怪的正则表达式问题

google-plus - window.googleapis未定义

colors - "Google Plus Red"是什么十六进制颜色代码?

oauth - 使用 google plus api 是否需要 SSL?