JavaScript String endsWith() Method – Fixing error “Object doesn’t support property or method ‘endsWith'” for not supported browsers (e.g. Internet Explorer 10, Internet Explorer 11)

In this article, we will see how we can fix the “Object doesn’t support property or method ‘endsWith’” error which was due to JavaScript (ECMAScript 6) String endsWith() Method not supported on browsers like IE 10, IE 11.

Fixing error “Object doesn’t support property or method ‘endsWith'”

Object doesn’t support property or method ‘endsWith

For fixing this error, you just need to include the  “endsWithForNotSupportedBrowser” method in your JavaScript file (common JavaScript file that is included on all pages) as shown below.

endsWithForNotSupportedBrowser Method: This method check if “String.prototype.endsWith” is available or not and if it is not available it will provide custom implementation for the same.

var Common = {
    init: function () {
        Common.endsWithForNotSupportedBrowser();
    },

    // endsWith for not supported browser. Eg. IE 10, IE 11.
    endsWithForNotSupportedBrowser: function () {
        if (!String.prototype.endsWith) {
            String.prototype.endsWith = function (searchValue, lengthToSearch) {
                if (lengthToSearch === undefined || lengthToSearch > this.length) {
                    lengthToSearch = this.length;
                }
                return this.substring(lengthToSearch - searchValue.length, lengthToSearch) === searchValue;
            };
        }
    },
};

$(function () {
    Common.init();
});

That’s it. Happy coding !!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *