最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Google Translate javascript snippet not working - Stack Overflow

programmeradmin5浏览0评论

I tried using the code snippet from w3school. It worked on w3school but doesnt work on my PC.

<div id="google_translate_element"></div>
<script>

function googleTranslateElementInit() {
    new google.translate.TranslateElement({
        pageLanguage: 'en'
    }, 'google_translate_element');
}

</script>
<script src="//translate.google/translate_a/element.js?cb=googleTranslateElementInit"></script>

I got the following in the console.

translate.html:18 GET file://translate.google/translate_a/element.js?cb=googleTranslateElementInit net::ERR_FILE_NOT_FOUND

I tried using the code snippet from w3school.. It worked on w3school but doesnt work on my PC.

<div id="google_translate_element"></div>
<script>

function googleTranslateElementInit() {
    new google.translate.TranslateElement({
        pageLanguage: 'en'
    }, 'google_translate_element');
}

</script>
<script src="//translate.google./translate_a/element.js?cb=googleTranslateElementInit"></script>

I got the following in the console.

translate.html:18 GET file://translate.google./translate_a/element.js?cb=googleTranslateElementInit net::ERR_FILE_NOT_FOUND
Share Improve this question edited Aug 7, 2019 at 22:31 Mitchell van Zuylen 4,1356 gold badges30 silver badges76 bronze badges asked Aug 7, 2019 at 20:18 devetokadevetoka 231 silver badge6 bronze badges 1
  • Try this one: github./mordadam/google-translate-html-js – mehmet Commented Jul 1, 2024 at 4:29
Add a ment  | 

3 Answers 3

Reset to default 5

The snippet over at w3school indeed has a bug. It says to add the following line to include Google's API:

 <script type="text/javascript" src="//translate.google./translate_a/element.js?cb=googleTranslateElementInit"></script> 

Unfortunately the trailing // makes it point to a local file. So unless you've downloaded the library and bundled it with your html file this points to nowhere. Instead link to the online library by adding https:

<script type="text/javascript" src="https://translate.google./translate_a/element.js?cb=googleTranslateElementInit"></script> 

When you're running it on your machine, you're running it as a local file. As such, the source file, which is loading from //translate.google etc, is trying to find this file on google.

If you replace this with:

<script type="text/javascript" src="https://translate.google./translate_a/element.js?cb=googleTranslateElementInit"></script> 

you will find it is no longer trying to find a local file (i.e. on your machine), but instead will look for it on the internet.

pure javascript, can be integrated from browser debug console, or in webview, or any site. In this example we just find element and change his content to button (you can change any element you want)

importScriptURI("https://translate.google./translate_a/element.js"); 
document.getElementsByTagName("h1")[0].innerHTML='<div id="google_translate_element"></div>';
setTimeout(()=>{ new google.translate.TranslateElement({pageLanguage: 'en'},'google_translate_element');},1000);
发布评论

评论列表(0)

  1. 暂无评论