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

时间:2022-09-11 15:16:20

I am checking the url to see if it contains or includes a ? in it to control the hash pop state in the window. All other browsers aren't having an issue only IE.

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

The debugger gives me this error when I try to load in this way Object doesn't support property or method 'includes'

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

I get no error when i load the page in tghrough the popstate

当我通过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')});
            };
        });
});

7 个解决方案

#1


149  

According to the MDN reference page, includes is not supported on Internet Explorer. The simplest alternative is to use indexOf, like this:

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

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

#2


34  

IE11 does implement String.prototype.includes so why not using the official Polyfill?

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

Source: polyfill source

来源:polyfill来源

#3


3  

I've used includes from Lodash which is really similar to the native.

我使用过来自Lodash的包含非常类似于原生的包含。

#4


2  

As in Internet Explorer, the javascript method "includes" doesn't support which is leading to the error as below

与在Internet Explorer中一样,javascript方法“includes”不支持导致错误的方法,如下所示

dijit.form.FilteringSelect TypeError: Object doesn't support property or method 'includes'

dijit.form.FilteringSelect TypeError:Object不支持属性或方法'includes'

So I have changed the JavaScript string method from "includes" to "indexOf" as below

所以我将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");
}

#5


0  

This question and its answers led me to my own solution (with help from SO), though some say you shouldn't tamper with native prototypes:

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

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

Then I just replaced all .includes() with .doesInclude() and my problem was solved.

然后我用.doesInclude()替换了所有.includes(),我的问题解决了。

#6


0  

I was facing similar issue while generating Keyboard event in IE and dispatching it to input box.

我在IE中生成键盘事件并将其发送到输入框时遇到了类似的问题。

So switched to jQuery and it is working fine, jQuery takes care of all inner details.

所以切换到jQuery并且它工作正常,jQuery负责所有内部细节。

#7


0  

I had a similar issue, and this has solved my problem big time! https://polyfill.io/

我有一个类似的问题,这解决了我的问题大时间! https://polyfill.io/

#1


149  

According to the MDN reference page, includes is not supported on Internet Explorer. The simplest alternative is to use indexOf, like this:

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

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

#2


34  

IE11 does implement String.prototype.includes so why not using the official Polyfill?

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

Source: polyfill source

来源:polyfill来源

#3


3  

I've used includes from Lodash which is really similar to the native.

我使用过来自Lodash的包含非常类似于原生的包含。

#4


2  

As in Internet Explorer, the javascript method "includes" doesn't support which is leading to the error as below

与在Internet Explorer中一样,javascript方法“includes”不支持导致错误的方法,如下所示

dijit.form.FilteringSelect TypeError: Object doesn't support property or method 'includes'

dijit.form.FilteringSelect TypeError:Object不支持属性或方法'includes'

So I have changed the JavaScript string method from "includes" to "indexOf" as below

所以我将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");
}

#5


0  

This question and its answers led me to my own solution (with help from SO), though some say you shouldn't tamper with native prototypes:

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

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

Then I just replaced all .includes() with .doesInclude() and my problem was solved.

然后我用.doesInclude()替换了所有.includes(),我的问题解决了。

#6


0  

I was facing similar issue while generating Keyboard event in IE and dispatching it to input box.

我在IE中生成键盘事件并将其发送到输入框时遇到了类似的问题。

So switched to jQuery and it is working fine, jQuery takes care of all inner details.

所以切换到jQuery并且它工作正常,jQuery负责所有内部细节。

#7


0  

I had a similar issue, and this has solved my problem big time! https://polyfill.io/

我有一个类似的问题,这解决了我的问题大时间! https://polyfill.io/