IE 7 and 8 Google Website Translator fails with Error: hasOwnProperty in element.js

IE 7 and 8 Google Website Translator fails with Error: hasOwnProperty in element.js

我们在多个网站上使用 Google 网站翻译器下拉菜单。几天前,我们注意到初始化下拉列表在 IE 7 和 IE 8 上显示 JavaScript 错误。

错误的确切来源在作为 http://translate.google.com/translate_a/element.js 返回的代码的第二行,其中显示 a.hasOwnProperty(b[c])?

确切的错误是"Object doesn't support property or method 'hasOwnProperty'"。

奇怪的是,在我们的一个网站上,翻译器下拉列表根本不显示,而在另一个网站上,每次加载页面时都会显示 JavaScript 错误。

任何建议如何:

是否有任何关于 Google 网站翻译器或专门的帮助论坛的详细文档?我找不到比这更详细的内容:https://support.google.com/translate/#topic=2534534

谢谢

抱歉交叉发帖。我从 Google 支持 here 那里得到了一些回应,但没有解决方案。

该问题影响 IE 7 和 IE 8,包括 运行 兼容模式下的更高 IE 版本或在另一个程序中使用 .NET Web 浏览器控件(两者均默认为 IE 7)。

我们现在禁用了 IE 7 和 8 的翻译实用程序,并使用 <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 强制 IE 在兼容模式和 Web 浏览器控件中使用最新的文档模式(注意,这不会更改用户代理这些情况下的字符串,仍然显示 MSIE 7,但使用最新的可用模式运行脚本,完整详细信息 here)

这样做我们仍然在 IE 10 和 IE 11 的嵌入式浏览器控件中遇到 "invalid parameter" 错误,但我们通过在初始化周围包装一个 try-catch 块来抑制向用户显示该错误。

我们的解决方法的完整实施如下所示:

<div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
   try {
        new google.translate.TranslateElement({ pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL, gaTrack: true, gaId: 'UA-xxxxxxxxx-x' }, 'google_translate_element');
    }
    catch(ex) {
       //this supresses JS errors in Google code being shown to users, the translate utility will simply not be shown
    }
}

try {
    //dynamically write link to translate JS file, only if useragent is not IE 7 or IE 8 or the document mode is bigger than IE 8
    if ((navigator.appVersion.indexOf("MSIE 7.") == -1 && navigator.appVersion.indexOf("MSIE 8.") == -1) || document.documentMode > 8) {       
        var newScript = document.createElement('script');
        newScript.type = 'text/javascript';
        newScript.src = '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
        document.body.appendChild(newScript);
    }
} catch (ex) {
    //ignore
}
</script>

这样我们就不会因为错误而打扰用户。嵌入式浏览器的翻译仍然失败,但如果 Google 进行进一步的更改,它仍有可能再次活跃起来...

刚遇到同样的问题。似乎他们正在使用 window 对象不支持的 hasOwnProperty。 (See this article).

The trouble with hasOwnProperty is that it does exist in IE8, but is not usable on the window object. If you’ve gone and written tonnes of code using commands like ‘window.hasOwnProperty(“somevar”)’, you might break down at the sight of it all failing in IE8. There’s a nice little fix for this:

使用以下代码解决问题。

window.hasOwnProperty = window.hasOwnProperty || Object.prototype.hasOwnProperty;

希望它也能解决 IE7 上的问题。