IE11 - 对象不支持属性或方法'includes' - javascript window.location.hash

Eri*_*rth 168 javascript internet-explorer-11 ecmascript-7

我正在检查网址,看它是否包含或包含?在其中控制窗口中的哈希弹出状态.所有其他浏览器只有IE的问题.

当我尝试以这种方式加载时,调试器给我这个错误Object不支持属性或方法'includes'

当我通过popstate加载页面时,我没有收到任何错误

    $(document).ready(function(e) {
        if(window.location.hash) {
            var hash;
            if(window.location.hash.includes("?")) {
                alert('I have a ?');
                hash = window.location.hash.substring(window.location.hash.indexOf('#') + 0,window.location.hash.indexOf('?'));
            }else {
                hash = window.location.hash;
            };
            if (hash=="#DRS" || hash=="#DRP" || hash=="#DFFI" || hash=="#DCI" || hash=="#DCP" || hash=="#DRP" || hash=="#DRMA" || hash=="#EICS" || hash=="#ORG"){
                $(hash+'Content').addClass('pageOn').removeClass('pageOff');
            }else {
                $('#homeContent').addClass('pageOn').removeClass('pageOff');
            };
        } else {
            $('#homeContent').addClass('pageOn').removeClass('pageOff');
        }
        $(window).on('popstate', function() {
            var hash;
            if(window.location.hash.includes("?")) {
                hash = window.location.hash.substring(window.location.hash.indexOf('#') + 0,window.location.hash.indexOf('?'));
            }else {
                hash = window.location.hash;
            };
            if (hash=="#DRS" || hash=="#DRP" || hash=="#DFFI" || hash=="#DCI" || hash=="#DCP" || hash=="#DRP" || hash=="#DRMA" || hash=="#EICS" || hash=="#ORG"){
                $(this).navigate({target: $(hash+'Content')});
                if(window.location.hash.includes("?")) {
                }else{
                    location.href = location.href+'?';
                }
            }else {
                $(this).navigate({target: $('#homeContent')});
            };
        });
});
Run Code Online (Sandbox Code Playgroud)

GOT*_*O 0 238

根据MDN参考页面,includesInternet Explorer不支持.最简单的替代方法是使用indexOf,如下所示:

if(window.location.hash.indexOf("?") >= 0) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • @troxwalt很好听,但在Windows 7 IE11上仍然无法使用! (2认同)

thi*_*goh 57

IE11确实实现了String.prototype.includes所以为什么不使用官方的Polyfill呢?

  if (!String.prototype.includes) {
    String.prototype.includes = function(search, start) {
      if (typeof start !== 'number') {
        start = 0;
      }

      if (start + search.length > this.length) {
        return false;
      } else {
        return this.indexOf(search, start) !== -1;
      }
    };
  }
Run Code Online (Sandbox Code Playgroud)

来源:polyfill来源


JCi*_*sar 33

添加import 'core-js/es7/array';到我polyfill.ts修复的问题为我.


bsh*_*eps 12

我在Angular项目中遇到了类似的问题。在我的polyfills.ts中,我必须同时添加:

    import "core-js/es7/array";
    import "core-js/es7/object";
Run Code Online (Sandbox Code Playgroud)

除了启用所有其他IE 11默认值。(如果使用角,请参见polyfills.ts中的注释)

添加这些导入后,错误消失了,并且按预期填充了我的对象数据。


小智 5

与Internet Explorer一样,javascript方法“ includes”不支持,这导致出现以下错误

dijit.form.FilteringSelect TypeError:对象不支持属性或方法“ includes”

因此,我将JavaScript字符串方法从“ includes”更改为“ indexOf”,如下所示

//str1 doesn't match str2 w.r.t index, so it will try to add object
var str1="acd", str2="b";
if(str1.indexOf(str2) == -1) 
{
  alert("add object");
}
else 
{
 alert("object not added");
}
Run Code Online (Sandbox Code Playgroud)