.attachClickHandler() does not work on page refresh/redirect(.attachClickHandler()在页面刷新/重定向时不起作用)
本文介绍了.attachClickHandler()在页面刷新/重定向时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在index.html中实现一个定制的Google登录按钮,它在页面加载时工作得很好,但在页面重新加载或从另一个页面重定向时却不能。在页面加载时,控制台语句1、2和3自动按顺序打印,4在登录时打印。当从另一个页面重定向或刷新时,控制台仅打印1和2,并且该按钮不可点击。包含要包含GAPI的脚本
<script src="aHR0cHM6Ly9hcGlzLmdvb2dsZS5jb20vanMvYXBpOmNsaWVudC5qcw=="></script>
在index.html中。我正在使用AngularJS,如果这与此有关的话。
var googleUser = {};
var startApp = function() {
console.log("1");
gapi.load('auth2', function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
console.log("2");
auth2 = gapi.auth2.init({
client_id: '*************.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: 'profile email'
});
setTimeout(function(){
attachSignin(document.getElementById('g_login'));
}, 1000);
// attachSignin(document.getElementById('g_login'));
});
};
function attachSignin(element) {
console.log("3");
auth2.attachClickHandler(element, {},
function(googleUser) {
console.log("4");
document.getElementById('name').innerText = "Signed in: " +
googleUser.getBasicProfile().getName();
angular.element(document.getElementById('status')).scope().googleLogin(googleUser.getAuthResponse().id_token);
}, function(error) {
alert(JSON.stringify(error, undefined, 2));
});
}
startApp();
</script>
推荐答案
已解决。
全局变量‘auth2’在每次加载页面时自动初始化,可用于用户登录。脚本调用方式为:
<script src="aHR0cHM6Ly9hcGlzLmdvb2dsZS5jb20vanMvYXBpOmNsaWVudC5qcz9vbmxvYWQ9b25Mb2FkQ2FsbGJhY2s=" async defer></script>
<script>
var auth2;
var googleUser = {};
var auth3;
window.onLoadCallback = function(){
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: '**********.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: 'profile email'
});
auth3 = true;
startApp();
})
}
var startApp = function() {
element = document.getElementById('g_login');
auth2.attachClickHandler(element, {},
function(googleUser) {
console.log("4");
document.getElementById('name').innerText = "Signed in: " +
googleUser.getBasicProfile().getName();
angular.element(document.getElementById('status')).scope().googleLogin(googleUser.getAuthResponse().id_token);
}, function(error) {
console.log("5");
alert(JSON.stringify(error, undefined, 2));
});
};
if (auth3)
startApp();
</script>
这篇关于.attachClickHandler()在页面刷新/重定向时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:.attachClickHandler()在页面刷新/重定向时不起作用


猜你喜欢
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01