SCRIPT438:对象在 IE10 中不支持 属性 或方法 'endsWith'
SCRIPT438: Object doesn't support property or method 'endsWith' in IE10
我有一个在 Chrome 中工作正常但在 IE10 中出现以下错误的函数
SCRIPT438: Object doesn't support property or method 'endsWith'
function getUrlParameter(URL, param){
var paramTokens = URL.slice(URL.indexOf('?') + 1).split('&');
for (var i = 0; i < paramTokens.length; i++) {
var urlParams = paramTokens[i].split('=');
if (urlParams[0].endsWith(param)) {
return urlParams[1];
}
}
}
谁能告诉我这个函数有什么问题吗?
实现endsWith
如下
String.prototype.endsWith = function(pattern) {
var d = this.length - pattern.length;
return d >= 0 && this.lastIndexOf(pattern) === d;
};
您应该使用以下代码在不支持它的浏览器中实现 endsWith
:
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
这直接来自 Mozilla Developer Network 并且符合标准,与目前给出的其他答案不同。
IE v.11 及以下版本不支持某些 ES6 属性,如 set、endsWith 等。因此您需要为各个 ES6 属性添加 polyfill。
为了简化这个过程,你可以使用一些编译器,比如 Babel JS 或者外部库,比如 polyfill.js 等
对于在您 index.html 中的标记之前或捆绑发生之前添加以下代码段的结尾。
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
我有一个在 Chrome 中工作正常但在 IE10 中出现以下错误的函数
SCRIPT438: Object doesn't support property or method 'endsWith'
function getUrlParameter(URL, param){
var paramTokens = URL.slice(URL.indexOf('?') + 1).split('&');
for (var i = 0; i < paramTokens.length; i++) {
var urlParams = paramTokens[i].split('=');
if (urlParams[0].endsWith(param)) {
return urlParams[1];
}
}
}
谁能告诉我这个函数有什么问题吗?
实现endsWith
如下
String.prototype.endsWith = function(pattern) {
var d = this.length - pattern.length;
return d >= 0 && this.lastIndexOf(pattern) === d;
};
您应该使用以下代码在不支持它的浏览器中实现 endsWith
:
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
这直接来自 Mozilla Developer Network 并且符合标准,与目前给出的其他答案不同。
IE v.11 及以下版本不支持某些 ES6 属性,如 set、endsWith 等。因此您需要为各个 ES6 属性添加 polyfill。 为了简化这个过程,你可以使用一些编译器,比如 Babel JS 或者外部库,比如 polyfill.js 等
对于在您 index.html 中的标记之前或捆绑发生之前添加以下代码段的结尾。
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}