When I tried this.form.name.endsWith("$END$")
in IE11, getting following error.
Object doesn't support property or method 'endsWith'
In Chrome, its working fine. Is there any alternative string method in IE11
When I tried this.form.name.endsWith("$END$")
in IE11, getting following error.
Object doesn't support property or method 'endsWith'
In Chrome, its working fine. Is there any alternative string method in IE11
Share Improve this question asked Apr 17, 2018 at 6:50 cpp_learnercpp_learner 3822 gold badges4 silver badges15 bronze badges 2-
String.prototype.endsWith = "".endsWith || function(s){return !this.split(s).pop();};
– dandavis Commented Apr 17, 2018 at 6:53 - for future reference ... check MDN documentation - it will tell you if a modern javascript method is patible with a Neanderthal browser – Jaromanda X Commented Apr 17, 2018 at 6:56
4 Answers
Reset to default 8You may use the polyfill instead
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;
};
}
Reference: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
In IE11 there is no endsWith implemented. You will have to use a polyfill like the one from mdn
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;
};
}
As laid out here and on MDN, IE11 does not support String.endsWith
. You can either use a polyfill - which adds support for endsWith onto the String object - or use existing JavaScript functions, for instance String.match
or RegExp.test
:
this.form.name.match(/\$END\$$\)
I prefer a shim cdn as I don't need to worry about another developer breaking the js. For Wordpress, use this.
wp_enqueue_script( 'polyfill', 'https://cdn.polyfill.io/v2/polyfill.min.js' , false, '2.0.0', true);