Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startsWith method not supported in IE any replacement for this? [duplicate]

var s = "Main String";
s.startsWith("Main");

Replacing startWith with a similar logic or any other method generic to all browsers.

like image 273
Chetan Raikwal Avatar asked Aug 04 '16 16:08

Chetan Raikwal


2 Answers

A good replacement for startsWith would be using s.substring(0, 4) == 'Main'

This should work, so go ahead and try it.

like image 176
Addisonep Avatar answered Oct 26 '22 23:10

Addisonep


Add a prototype method in String:

String.prototype.myStartsWith = function(str){
 if(this.indexOf(str)===0){
  return true;
 }else{
   return  false;
 }
};

Now call:

s.myStartsWith("Main");
like image 28
Tuhin Avatar answered Oct 27 '22 01:10

Tuhin