javascript SCRIPT438:对象不支持 IE10 中的属性或方法“endsWith”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29434868/
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
SCRIPT438: Object doesn't support property or method 'endsWith' in IE10
提问by RanPaul
I have a below function which works fine in Chrome but its giving the below error in IE10
SCRIPT438: Object doesn't support property or method 'endsWith'
我有一个以下功能,它在 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];
}
}
}
Can someone tell me whats wrong with this function?
有人能告诉我这个功能有什么问题吗?
回答by RanPaul
Implemented endsWith
as below
实现endsWith
如下
String.prototype.endsWith = function(pattern) {
var d = this.length - pattern.length;
return d >= 0 && this.lastIndexOf(pattern) === d;
};
回答by ErikE
You should use the following code to implement endsWith
in browsers that don't support it:
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;
};
}
This is directly from Mozilla Developer Networkand is standards-compliant, unlike the other answer given so far.
这直接来自Mozilla Developer Network,并且符合标准,与目前给出的其他答案不同。
回答by abhijit padhy
IE v.11 and below does not support some of the ES6 properties like set, endsWith etc. So you need to add polyfills for individual ES6 properties. To ease the process you can use some compiler like Babel JSor external libraries like polyfill.jsetc.
IE v.11 及以下版本不支持某些 ES6 属性,如 set、endsWith 等。因此您需要为各个 ES6 属性添加 polyfill。为了简化这个过程,你可以使用一些编译器(如Babel JS)或外部库(如 polyfill.js等)。
For ends with add below snippet before the tag in your index.html or before bundling happens.
对于在 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;
};
}