最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to add onload event for gapi? - Stack Overflow

matteradmin6PV0评论

I am using Google API (gapi) for user log in.

My code is below. It loads google sdk asynchronously. Once it is loaded I need to call the api function gapi.auth.authorize

(function(d: any, s: string, id: string) {
    var js: HTMLScriptElement, gjs: Element = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = ".js";
    gjs.parentNode.insertBefore(js, gjs);

    if (js.readyState){  //IE
        js.onreadystatechange = function(){
            if (js.readyState == "loaded" ||
                js.readyState == "plete"){
                js.onreadystatechange = null;
                gapi.auth.authorize();   //<-- details omitted 
            }
        };
    } else {  //Others
        js.onload = function(){
            gapi.auth.authorize();   //<-- details omitted 
        };
    }
}(document, 'script', 'google-jssdk'));

Now the issue is - I get error

TypeError: gapi.auth is undefined

It should be defined right? I looked at the console, and typed gapi.auth and I get an object in response.

So I believe that js.onload event is getting triggered early, i.e when gapi.auth is not ready.

How to fix this? Or more specifically how to add onload event for gapi?

I am using Google API (gapi) for user log in.

My code is below. It loads google sdk asynchronously. Once it is loaded I need to call the api function gapi.auth.authorize

(function(d: any, s: string, id: string) {
    var js: HTMLScriptElement, gjs: Element = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "https://apis.google./js/client.js";
    gjs.parentNode.insertBefore(js, gjs);

    if (js.readyState){  //IE
        js.onreadystatechange = function(){
            if (js.readyState == "loaded" ||
                js.readyState == "plete"){
                js.onreadystatechange = null;
                gapi.auth.authorize();   //<-- details omitted 
            }
        };
    } else {  //Others
        js.onload = function(){
            gapi.auth.authorize();   //<-- details omitted 
        };
    }
}(document, 'script', 'google-jssdk'));

Now the issue is - I get error

TypeError: gapi.auth is undefined

It should be defined right? I looked at the console, and typed gapi.auth and I get an object in response.

So I believe that js.onload event is getting triggered early, i.e when gapi.auth is not ready.

How to fix this? Or more specifically how to add onload event for gapi?

Share Improve this question asked Oct 3, 2014 at 8:41 Vinayak GargVinayak Garg 6,61410 gold badges56 silver badges82 bronze badges 1
  • I have the same problem. I am using chrome on mac os. I am loading synchronously and when I type gapi.auth into the console it is still undefined and when I type gapi I get an object with many properties such as client, plus, etc. but no auth. Does this have something to do with the APIs enabled from the developer console? All I enabled was the Google+ API. – Nick Sotiros Commented Oct 7, 2015 at 18:27
Add a ment  | 

3 Answers 3

Reset to default 3

What browser is this seen in? Also, you need to include a ?onload=myFunction parameter at the end of the script's src attribute, then implement window['myFunction'] = function() { ... } to detect library readiness. The loading typically injects an additional script node and you'll need to wait for that one, too.

window.gapi_onload = function() { gapi.auth.authorize(); }

You can also try the package gapi-script, this package provides the gapi instantly, so you don't have to wait any script to load, just import it and use:

import { gapi } from 'gapi-script';

And the package also has a function to initialize the gapi auth2:

import { loadAuth2 } from 'gapi-script';

let auth2 = await loadAuth2(clientId, scopes);
Post a comment

comment list (0)

  1. No comments so far