3

我與谷歌JavaScript客戶端API玩,我想知道的代碼以下兩個部件之間的區別是什麼的正確方法:什麼是加載谷歌JavaScript客戶端庫和自定義端點客戶端庫

//in the html file 
<script src="https://apis.google.com/js/client.js?onload=gapiInit"></script> 

//in a javscript file 
gapiInit = function() { 
    alert("Loading the Libs!"); 

    var numApisToLoad; 

    var callback = function(){ 
     if(--numApisToLoad == 0) { 
      alert('APIs Ready!'); 
      } 
    }; 

    numApisToLoad = 1; 

    gapi.client.load('plus','v1',callback); 
}; 

這..

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

我已經從不同的例子,谷歌提供的谷歌加號例如,在使用後者,而云終端的例子使用前見過..

他們只是兩種做同樣的事情嗎?我真的很重要嗎?從developer.google.com

//端點

例子 -
https://developers.google.com/appengine/docs/java/endpoints/consume_js

//加上登錄(似乎兩者都做) -
https://developers.google.com/+/web/people/

//只async script .. -
developers.google.com/+/web/api/javascript

(對於狡猾的鏈接對不起,堆棧溢出不會讓我把超過兩個)

任何洞察力將不勝感激。

謝謝大家。

回答

5

此:

<script src="https://apis.google.com/js/client.js?onload=gapiInit"></script> 

,然後繼續完全加載client.js。確保在頁面上的所有標記之後放置它以提高性能。

雖然此:

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

好像它會(在路徑通知客戶端:Plusone精選)加載在客戶端和Plusone精選JS模塊。它會在後臺加載。該網址似乎不過是缺少一個回調:

https://apis.google.com/js/client:plusone.js?onload=onLoadCallback 

這樣做,如果你想以後還是有條件地加載它的好處。完成加載後,它將調用您必須定義的onLoadCallback函數。

https://developers.google.com/+/web/api/javascript

+0

三江源的洞察力,所以看起來它們導致在客戶端和加模塊被加載相同的「國家」 ......? –

+2

或者只是在腳本標籤中使用'async defer' ...不需要恐慌 – vsync