首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在IE11中使用`window.location.hash.includes`抛出“对象不支持属性或方法‘includes’”

在IE11中使用`window.location.hash.includes`抛出“对象不支持属性或方法‘includes’”
EN

Stack Overflow用户
提问于 2015-06-29 23:12:39
回答 8查看 142.6K关注 0票数 175

我正在检查URL,看看它是否包含或包含一个?,以控制窗口中的散列弹出状态。所有其他浏览器都没有问题,只有IE有问题。

当我尝试以这种方式加载时,调试器显示以下错误:

对象不支持属性或方法“”includes“”

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

代码语言:javascript
复制
    $(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')});
            };
        });
});
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2015-06-29 23:21:44

根据MDN reference page的说法,Internet Explorer不支持includes。最简单的替代方法是使用indexOf,如下所示:

代码语言:javascript
复制
if(window.location.hash.indexOf("?") >= 0) {
    ...
}
票数 243
EN

Stack Overflow用户

发布于 2017-02-02 09:12:06

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

代码语言:javascript
复制
  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;
      }
    };
  }

来源:polyfill source

票数 57
EN

Stack Overflow用户

发布于 2018-10-03 23:32:28

为我的polyfill.ts添加import 'core-js/es7/array';解决了这个问题。

票数 37
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31119300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档