Javascript 为您的网站异步加载Google Translate小部件的方法?

Javascript 为您的网站异步加载Google Translate小部件的方法?,javascript,asynchronous,google-translate,Javascript,Asynchronous,Google Translate,有没有一种方法可以异步加载你网站的谷歌翻译小部件 我试着把它放在我的页面底部,但是#google#u translate#元素容器仍然是空的 <!-- Asynchronous Google Translate --> <script type="text/javascript"> function googleTranslateElementInit() { new google.translate.TranslateElement({pageLanguage: '

有没有一种方法可以异步加载你网站的谷歌翻译小部件

我试着把它放在我的页面底部,但是#google#u translate#元素容器仍然是空的

<!-- 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>

函数googleTranslateElementInit(){
新的google.translate.TranslateElement({pageLanguage:'en',包括语言:'ar、bg、bn、de、el、eo、es、en、fr、hi、id、it、iw、ja、ko、pl、pt、ru、th、tr、vi、zh CN',布局:google.TranslateElement.InlineLayout.SIMPLE、gaTrack:true、gaId:'UA-1234-1','google_translate_元素');
}
(功能(){
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);
})();

您的代码似乎有几个问题。但你的基本想法是正确的

假设
是在脚本标记之前定义的,则以下操作应该有效:

<!-- 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>

函数googleTranslateElementInit(){
新的google.translate.TranslateElement({pageLanguage:'en',
包括语言:“ar、bg、bn、de、el、eo、es、en、fr、hi、id、it、iw、ja、ko、pl、pt、ru、th、tr、vi、zh CN”,
布局:google.translate.TranslateElement.InlineLayout.SIMPLE,
盖特拉克:没错,盖德:“UA-37652767-1’,“谷歌翻译元素”);
}
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);
至少,当我把它全部放在一个HTML文件中并加载到chrome浏览器中时,它对我起了作用


当然,可以将
var
声明和以下行放在
$(document.ready
函数中(如果不使用jQuery,也可以用其他方式)。那么
div
script
之间的顺序将不再重要。

谢谢。你能“同行评审”我的编辑吗?我将src改进为只//而不是http://。这样,它也可以使用HTTPS。这是一个巧妙的技巧。我还没有让代表回顾,否则我会的。啊-
/
对我不起作用,但那可能是因为我没有在服务器上安装它。实际上,这看起来很不错!由于IE的支持,我建议使用延迟而不是异步。好吧,我读了一些资料,结果表明,在IE9使用非标准的延迟实现之前,Opera和IE4及以上版本都不支持延迟和异步,所以它可能不会像规范所说的那样工作。您可以查看脚本标记及其属性的浏览器兼容性。