JavaScript endsWith is not working in IEv10?
I found the simplest answer,
All you need do is to define the prototype
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
It's generally bad practise to extend the prototype of a native JavaScript object. See here - Why is extending native objects a bad practice?
You can use a simple check like this that will work cross-browser:
var isValid = (string1.lastIndexOf(string2) == (string1.length - string2.length))
Method endsWith()
not supported in IE. Check browser compatibility here.
You can use polyfill option taken from MDN documentation:
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;
};
}