Javascript 在 IE11 中使用 `window.location.hash.includes` 会抛出“对象不支持属性或方法 'includes'”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31119300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using `window.location.hash.includes` throws “Object doesn't support property or method 'includes'” in IE11
提问by Erik Grosskurth
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.
我正在检查 URL 以查看它是否包含或包含一个?
来控制窗口中的哈希弹出状态。所有其他浏览器都没有问题,只有 IE。
The debugger gives me this error when I try to load in this way:
当我尝试以这种方式加载时,调试器给了我这个错误:
Object doesn't support property or method '
includes
'
对象不支持属性或方法“
includes
”
I get no error when I load the page in through 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')});
};
});
});
回答by GOTO 0
According to the MDN reference page, includes
is not supported on Internet Explorer. The simplest alternative is to use indexOf
, like this:
根据MDN 参考页面,includes
Internet Explorer 不支持。最简单的替代方法是使用indexOf
,如下所示:
if(window.location.hash.indexOf("?") >= 0) {
...
}
回答by thiagoh
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 来源
回答by JCisar
Adding import 'core-js/es7/array';
to my polyfill.ts
fixed the issue for me.
添加import 'core-js/es7/array';
到我为我polyfill.ts
解决的问题。
回答by bsheps
I had a similar issue with an Angular project. In my polyfills.ts I had to add both:
我在 Angular 项目中遇到了类似的问题。在我的 polyfills.ts 中,我必须同时添加:
import "core-js/es7/array";
import "core-js/es7/object";
In addition to enabling all the other IE 11 defaults. (See comments in polyfills.ts if using angular)
除了启用所有其他 IE 11 默认值。(如果使用 angular,请参阅 polyfills.ts 中的注释)
After adding these imports the error went away and my Object data populated as intended.
添加这些导入后,错误消失了,我的对象数据按预期填充。
回答by satish hiremath
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: 对象不支持属性或方法“包含”
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");
}
回答by Moacir Rosa
回答by Bhushan Babar
I'm using ReactJs and used import 'core-js/es6/string';
at the start of index.js
to solve my problem.
我正在使用 ReactJs 并import 'core-js/es6/string';
在开始时index.js
用于解决我的问题。
I'm also using import 'react-app-polyfill/ie11';
to support running React in IE11.
我也import 'react-app-polyfill/ie11';
用来支持在 IE11 中运行 React。
react-app-polyfill
This package includes polyfills for various browsers. It includes minimum requirements and commonly used language features used by Create React App projects.
react-app-polyfill
该软件包包括适用于各种浏览器的 polyfill。它包括 Create React App 项目使用的最低要求和常用语言功能。
https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md
https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md
回答by TecBrat
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.
然后我只是替换了所有.includes()
,.doesInclude()
我的问题就解决了。