RanPaul RanPaul - 15 days ago 13
Javascript Question

SCRIPT438: Object doesn't support property or method 'endsWith' in IE10

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'


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?

Answer

Implemented endsWith as below

String.prototype.endsWith = function(pattern) {
  var d = this.length - pattern.length;
  return d >= 0 && this.lastIndexOf(pattern) === d;
};