Add startsWith in IE 11

ie11 startswith polyfill
javascript startswith alternative
startswith alternative in ie
object doesn't support property or method 'startswith' react ie11
indexof
object doesn't support property or method 'startswith' babel
javascript substring
how to use polyfill in javascript

IE 11 does not support startsWith with strings. (Look here)

How do you add a prototype so that it supports the method?

Straight from the MDN page, here's the polyfill:

if (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        value: function(search, rawPos) {
            var pos = rawPos > 0 ? rawPos|0 : 0;
            return this.substring(pos, pos + search.length) === search;
        }
    });
}

This is safe to use in any browser. If the method already exists, this code will see that and do nothing. If the method does not exist, it will add it to the String prototype so it is available on all strings.

You just add this to one of your JS files in some place where it executes at startup and before you attempt to use .startsWith().

Code not running in IE 11, works fine in Chrome, String.prototype.startsWith is a standard method in the most recent version of JavaScript, ES6. Looking at the compatibility table below, we can� Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

Found an easier way to fix this,

function startsWith(str, word) {
    return str.lastIndexOf(word, 0) === 0;
}

like wise to find the endswith use below code,

function endsWith(str, word) {
    return str.indexOf(word, str.length - word.length) !== -1;
}

JavaScript 'startsWith' method not supported in Internet Explorer 11, JavaScript 'startsWith' method not supported in Internet Explorer 11. here, which can be added as a static resource and added as a dependency where needed. Fix for IE11 check before `preserveToString` When in IE11 the 'preserve string' method crashes on `startsWith`. `startsWith` doesn't exist in IE11 so the `preserveToString` method doesn't work (there's nothing to preserve).

I've been using this polyfill:

if (typeof String.prototype.startsWith != 'function') {
    String.prototype.startsWith = function(prefix) {
        return this.slice(0, prefix.length) == prefix;
    };
}

IE11 support: Stop using `startsWith` in the JS codebase � Issue , IE11 support: Stop using `startsWith` in the JS codebase #8944. Closed timabbott added bug area: browser-support labels on Apr 3, 2018. IE11 will never be brought up to date because Microsoft are no longer adding new features to it - it will just get security patches between now and when Microsoft abandon Internet Explorer completely.

I've reviewed and edited my answer. I believe that my code is now much more appropriate than before.

If your browser supports built-in 'startsWith' function then that built-in function will be use. Additionally I've included 'endsWith' also.

if (typeof String.prototype.startsWith !== 'function') {
   String.prototype.startsWith = function(searchString, position) {	
       position = position || 0;
       return this.substring(position,  position + searchString.length) === searchString;
   }
}


if (typeof String.prototype.endsWith !== 'function') {
   String.prototype.endsWith = function(searchString, strtLength) {	
      strtLength = (strtLength === undefined || strtLength > this.length)? this.length : strtLength;  	
      return this.substring(strtLength - searchString.length, strtLength) === searchString;
   }
}

  

/* usage */

let str = 'To be, or not to be, that is the question.'

console.log(str.startsWith('To be'))          // true
console.log(str.startsWith('not to be'))      // false
console.log(str.startsWith('not to be', 10))  // true

let str2 = 'To be, or not to be, that is the question.'
console.log(str2.endsWith('question.'))  // true
console.log(str2.endsWith('to be'))      // false
console.log(str2.endsWith('to be', 19))  // true

IE 11 compatibility issue due to startsWith � Issue #168 , Should work in IE since the docs do not mention adding a polyfill for String. prototype.startsWith and seems like it was fixed in a previous version� The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate. The source for this interactive example is stored in a GitHub repository.

String.prototype.startsWith(), Polyfill. This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. * Original Title: Adding Addons to IE 11 . I am using Windows 8.1 and IE11. I can go to the Addon Gallery and select the Addon that I want and it brings up a page containing a button "Add to Internet Explorer" and my mouse cursor changes to a hand when I move it to the button. however, the button does NOTHING no matter how many times I click it!

Code not running in IE 11, works fine in Chrome, object doesn t support property or method 'endswith ie11 Fix Internet Browser Display Issues, Install the "IETab" browser extension for Chrome or IE Tab for� Also, is IE running in compatibility mode, or running as proper IE 11? Line 37 when I open it is an empty line with only comments, from what I can see, and I don't have an IE11 installation to test with.

IE 11: Object doesn't support property or method 'startsWith , Hi, first thanks for a great plugin. Latest release version throw the following error in IE 11: Object doesn't support property or method 'startsWith'. Internet Explorer� The Favorites bar gives you one-click access to your favorite websites. It is hidden by default in IE 11. To reveal the Favorites bar, right-click the settings gear and select Favorites Bar. To add the active web page to the Favorites bar, select the star with a green arrow on the left side of the Favorites bar.

Comments
  • For future reference and to hopefully help future searches: The error that appears in IE is: TypeError: Object doesn't support property or method 'startsWith'
  • Do you just add it straight to the JS file?
  • people should def look there they usually always have a nice polyfill
  • @SandahAung yes before you want to use it
  • substr() function is considered a legacy function according to mozilla developer: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. If possible, use the substring() method instead (Recommended by MDN).
  • @fatih - MDN has apparently updated their polyfill to use .substring() so I edited the answer to reflect that.