Javascript 使用'window.location.hash.includes'throws“;对象没有';t支持属性或方法';包括'”;在IE11中

Javascript 使用'window.location.hash.includes'throws“;对象没有';t支持属性或方法';包括'”;在IE11中,javascript,internet-explorer-11,ecmascript-2016,Javascript,Internet Explorer 11,Ecmascript 2016,我正在检查URL,查看它是否包含或包含?,以控制窗口中的哈希弹出状态。所有其他浏览器都没有问题,只有IE 当我尝试以这种方式加载时,调试器会出现以下错误: 对象不支持属性或方法“包含”” 当我通过popstate加载页面时,没有收到任何错误 $(document).ready(function(e) { if(window.location.hash) { var hash; if(window.location.hash.

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

当我尝试以这种方式加载时,调试器会出现以下错误:

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

当我通过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')});
            };
        });
});
根据,Internet Explorer不支持
包括
。最简单的替代方法是使用
indexOf
,如下所示:

if(window.location.hash.indexOf("?") >= 0) {
    ...
}

在InternetExplorer中,javascript方法“includes”不支持以下错误

dijit.form.FilteringSelect类型错误:对象不支持属性或方法“includes”

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


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;
      }
    };
  }

来源:

我使用了与本机非常相似的
Lodash

这个问题及其答案让我找到了自己的解决方案(在SO的帮助下),尽管有人说你不应该篡改本机原型:

  // IE does not support .includes() so I'm making my own:
  String.prototype.doesInclude=function(needle){
    return this.substring(needle) != -1;
  }

然后我将所有
.include()
替换为
.doesInclude()
,我的问题就解决了。

添加
导入'core js/es7/array'到我的
polyfill.ts
为我修复了这个问题。

我有一个类似的角度项目问题。在我的polyfills.ts中,我必须添加以下两项:

    import "core-js/es7/array";
    import "core-js/es7/object";
除了启用所有其他IE11默认设置之外。(如果使用角度,请参见polyfills.ts中的注释)


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

我使用的是ReactJs,使用的是
导入'core js/es6/string'
index.js
的开头解决我的问题

我也在使用
导入'react-app-polyfill/ie11'以支持在IE11中运行React

react app polyfill

此软件包包括用于各种用途的polyfills 浏览器。它包括最低要求和常用语言 创建应用程序项目使用的功能


internet explorer 11中
window.location.hash
的值是多少?好吧,如果(window.location.hash.indexOf(“?”>)=0{//do nothing}或者{location.href=location.href+'?';}我需要添加一个?如果url没有一个现成的问题和答案,我知道这是一个老问题和答案,但是String.prototype.includes似乎在Windows 10 IE 11上对我有效。@troxwalt听上去不错,但在Windows 7 IE11上仍然不起作用!如果您正在反应,可以使用并避免手动添加polyfills…
。includes()
对字符串和数组都有效。您的
substring()
解决方案仅对字符串有效,而对数组无效。正如其他人所回答的,使用
indexOf()
而不是
substring()
更好,因为这两种情况都适用。您还可以使用core js中的polyfill,可以在这里找到:@DavidBarreto!这是很久以前发布的,但也许这对新框架上的人会有所帮助,但是我认为选择的答案应该足以满足特殊用例之外的任何情况;从2020I开始手动填充IE11控制台中出现故障的所有功能。。。非常感谢你节省时间的回答。要同时将这两个包添加到您的包中。json:
npm i--save core js react app polyfill
Btw,
core js/es6
似乎不再存在于v3中,所以就这样吧。这对我来说很有效。这两种进口产品到底有什么用?。什么导入修复了包含错误?。或者这两个em都是针对includes错误的?由于IE11不再接收Microsoft的功能更新,javascript实现已经过时,并且只支持通过ES5的方法。通过导入这2个文件,您将通过ES7为数组和对象添加polyfill脚本,其中包括“includes”方法。
    import "core-js/es7/array";
    import "core-js/es7/object";