admin管理员组文章数量:1410717
I am playing with the Google Javascript client API and am wondering what the difference between the two following pieces of code is:
//in the html file
<script src=".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);
};
and this..
<script>
// Asynchronously load the client library
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = ':plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
I have seen both in various examples that google provides, the Google Plus Sign in example use the latter while the cloud endpoint examples use the former..
Are they just two ways of doing the exact same thing? and does it really matter which I use?
Examples from developer.google
//Endpoints --
//Plus Login (seems to do both) --
/+/web/people/
//just the async script.. --
developers.google/+/web/api/javascript
(sorry for the dodgy link, stack overflow would not let me put more than two in)
Any insight would be much appreciated.
Thanks everyone.
I am playing with the Google Javascript client API and am wondering what the difference between the two following pieces of code is:
//in the html file
<script src="https://apis.google./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);
};
and this..
<script>
// Asynchronously load the client library
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google./js/client:plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
I have seen both in various examples that google provides, the Google Plus Sign in example use the latter while the cloud endpoint examples use the former..
Are they just two ways of doing the exact same thing? and does it really matter which I use?
Examples from developer.google.
//Endpoints --
https://developers.google./appengine/docs/java/endpoints/consume_js
//Plus Login (seems to do both) --
https://developers.google./+/web/people/
//just the async script.. --
developers.google./+/web/api/javascript
(sorry for the dodgy link, stack overflow would not let me put more than two in)
Any insight would be much appreciated.
Thanks everyone.
Share Improve this question asked May 9, 2014 at 0:03 Luke KentwellLuke Kentwell 552 silver badges7 bronze badges1 Answer
Reset to default 5This:
<script src="https://apis.google./js/client.js?onload=gapiInit"></script>
Will fully load client.js before continuing. Make sure you put it after all markup on the page to increase performance.
While this:
<script>
// Asynchronously load the client library
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google./js/client:plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
Seems like it will load both the client and plusone js modules (notice in the path the client:plusone). It will load in the background. The url seems to be missing a callback though:
https://apis.google./js/client:plusone.js?onload=onLoadCallback
This has the benefit of if you wanted to load it later or conditionally. When its done loading it will call the onLoadCallback function that you must define.
See https://developers.google./+/web/api/javascript
本文标签:
版权声明:本文标题:What is the Proper way to load the Google Javascript Client Library and a custom Endpoint Client Library - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744825810a2627053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论