. attachclickhandler()在页面刷新/重定向时不起作用
.attachClickHandler() does not work on page refresh/redirect
我试图在index.html中实现一个自定义的谷歌登录按钮,该按钮在页面加载上完美工作,但当页面重新加载或从另一个页面重定向时却没有。在页面加载时,自动顺序打印控制台语句1、2和3,登录时打印控制台语句4。当从另一个页面重定向或刷新时,控制台只打印1和2,并且按钮不可点击。包含gapi的脚本包含在
中<script src="https://apis.google.com/js/api:client.js"></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="https://apis.google.com/js/api:client.js?onload=onLoadCallback" 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>
相关文章:
- 在phonegap应用程序内部重定向不起作用
- 角度 JS + 重定向不起作用
- 从帖子返回后,Javascript重定向不起作用
- 重定向不起作用
- 登录后PHP登录脚本重定向不起作用
- 使用window.location.replace重定向不起作用
- HTML POST->重定向工作,XMLHTTPrequest->重定向不起作用(无法从null发出任何请求
- Javascript window.location.href重定向不起作用
- JavaScript确认重定向不起作用
- 按钮重定向不起作用
- 使用AJAX从服务器获取数据后,如何重定向到另一个URL?URL重定向不起作用
- 如果 javascript 条件为 true 则重定向不起作用
- 页面刷新时的页面重定向不起作用
- 重定向不起作用/警报无显示
- Firefox的post重定向不起作用
- 清除字段按钮(重定向不起作用)
- Angular重定向不起作用
- Angular.js -重定向不起作用
- 使用servlet的重定向不起作用
- 使用document ready重定向不起作用