You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Edge浏览器IE模式下Google reCAPTCHA api.js最新变更不兼容求助

解决Edge IE模式(IE11)下Google reCAPTCHA加载失败的问题

问题根源是IE11原生不支持Element.prepend()方法,而Google近期更新的reCAPTCHA脚本中用到了这个ES6特性,导致在Edge IE模式下加载报错。

直接用以下方案修复:

  • 提前注入prepend方法的兼容垫片(polyfill),在加载reCAPTCHA脚本之前执行,确保reCAPTCHA代码运行时该方法已存在。

具体代码实现:

<!-- 先加载prepend兼容垫片 -->
<script>
if (!Element.prototype.prepend) {
    Element.prototype.prepend = function() {
        var argArr = Array.prototype.slice.call(arguments);
        var docFrag = document.createDocumentFragment();
        
        argArr.forEach(function(argItem) {
            var isNode = argItem instanceof Node;
            docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
        });
        
        this.insertBefore(docFrag, this.firstChild);
    };
}
</script>
<!-- 再加载Google reCAPTCHA脚本 -->
<script src='https://www.google.com/recaptcha/api.js' async defer></script>

说明:

  1. 这段垫片代码会先检查浏览器是否支持prepend方法,只有IE11这类不支持的浏览器才会执行兼容逻辑,不会影响现代浏览器的正常运行。
  2. 垫片完全模拟了标准prepend方法的功能:支持添加DOM节点或文本内容,并且会把内容插入到当前元素的第一个子节点位置。
  3. 如果你的项目已经引入了core-js这类ES6兼容库,也可以直接引入对应的polyfill模块替代上面的代码,效果一致。

内容的提问来源于stack exchange,提问作者Anurag Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:22:43