Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 381
RanPaul Avatar asked Apr 03 '15 15:04

RanPaul


2 Answers

Implemented endsWith as below

String.prototype.endsWith = function(pattern) {
  var d = this.length - pattern.length;
  return d >= 0 && this.lastIndexOf(pattern) === d;
};
like image 130
RanPaul Avatar answered Oct 03 '22 06:10

RanPaul


You should use the following code to implement endsWith in browsers that don't support it:

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;
    };
}

This is directly from Mozilla Developer Network and is standards-compliant, unlike the other answer given so far.

like image 43
ErikE Avatar answered Oct 03 '22 07:10

ErikE