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'”
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 !!!
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS