javascript - Way to asynchronously load Google Translate widget for your website? -


is there way asynchronously load google translate widget website?

i tried putting on bottom of page, #google_translate_element container still empty.

<!-- asynchronous google translate --> <script type="text/javascript"> function googletranslateelementinit() {   new google.translate.translateelement({pagelanguage: 'en', includedlanguages: 'ar,bg,bn,de,el,eo,es,en,fr,hi,id,it,iw,ja,ko,pl,pt,ru,th,tr,vi,zh-cn', layout: google.translate.translateelement.inlinelayout.simple, gatrack: true, gaid: 'ua-1234-1'}, 'google_translate_element'); }  (function() {   var googletranslatescript = document.createelement('script');   googletranslatescript.type = 'text/javascript';   googletranslatescript.async = true;   googletranslatescript.src = '//translate.google.com/translate_a/element.js?cb=googletranslateelementinit';   ( document.getelementsbytagname('head')[0] || document.getelementsbytagname('body')[0] ).appendchild( googletranslatescript ); })(); </script> 

it seems have several problems in code. basic idea sound.

assuming <div id="google_translate_element"></div> defined before the script tag, following should work:

<!-- asynchronous google translate --> <script type="text/javascript">   function googletranslateelementinit() {     new google.translate.translateelement({pagelanguage: 'en',       includedlanguages: 'ar,bg,bn,de,el,eo,es,en,fr,hi,id,it,iw,ja,ko,pl,pt,ru,th,tr,vi,zh-cn',       layout: google.translate.translateelement.inlinelayout.simple,       gatrack: true, gaid: 'ua-37652767-1'}, 'google_translate_element');   }    var googletranslatescript = document.createelement('script');   googletranslatescript.type = 'text/javascript';   googletranslatescript.async = true;   googletranslatescript.src = 'http://translate.google.com/translate_a/element.js?cb=googletranslateelementinit';   ( document.getelementsbytagname('head')[0] || document.getelementsbytagname('body')[0] ).appendchild(googletranslatescript); </script> 

at least worked me when placed in html file , loaded chrome.

of course possible place vardeclaration , following lines in $(document).ready function (or in other manner if not use jquery). order between div , script no longer of consequence.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -