Google translate element - load after page ready

ε祈祈猫儿з 提交于 2019-12-20 07:48:19

问题


I am using the google web translate element on my page. For those that don't know what it is you can find it here: http://translate.google.com/translate_tools

It loads on the page using javascript. I have it embedded on the top of my page which causes the rest of my content to stop loading until the translate bar has completed it's load.

How can I delay the javascript running until my page has fully loaded??

This is the script:

<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    includedLanguages: 'da,nl,en,fi,fr,it,no,ru,es,sv',
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE
  }, 'google_translate_element');
}
</script><script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

回答1:


Another method would be to load google translate asynchronously.

<div class="custom-translate" id="google_translate_element"></div>

<!-- ASYNCHRONOUS Google Translate -->
        <script type="text/javascript">
        function googleTranslateElementInit() {
          new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE, autoDisplay: false},'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>   
    <!-- End script -->



回答2:


As commented by John Conde, I put the script at the bottom of the page and hey presto, page load



来源:https://stackoverflow.com/questions/7986344/google-translate-element-load-after-page-ready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!