我正在检查URL,看看它是否包含或包含一个?
,以控制窗口中的散列弹出状态。所有其他浏览器都没有问题,只有IE有问题。
当我尝试以这种方式加载时,调试器显示以下错误:
对象不支持属性或方法“”
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')});
};
});
});
发布于 2015-06-29 15:21:44
根据MDN reference page的说法,Internet Explorer不支持includes
。最简单的替代方法是使用indexOf
,如下所示:
if(window.location.hash.indexOf("?") >= 0) {
...
}
发布于 2017-02-02 01:12:06
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;
}
};
}
发布于 2018-10-03 15:32:28
为我的polyfill.ts
添加import 'core-js/es7/array';
解决了这个问题。
https://stackoverflow.com/questions/31119300
复制