beginner
beginner

Reputation: 2508

str.startsWith() not working on android phone

I am using javascript's function .startsWith() for check if string starts with /assets

Code is

 var str = '/assets/image';
 if(str.startsWith('/assets')){alert('done');}

This works fine on pc , But not working on android phone , is .startsWith() has any compatibility issues on different browser or devices,and if yes ,then please tell me if any alternative for startsWith()

Thanks

Upvotes: 0

Views: 759

Answers (3)

Rafael
Rafael

Reputation: 18522

startsWith is a pretty new addition to JavaScript and is not broadly supported on mobile browsers.

As basic alternative you can use

str.indexOf(strToFind) === 0

Or a polyfill presented on Mozilla Developer Network

Upvotes: 2

Mohamed Abbas
Mohamed Abbas

Reputation: 2288

Try this, it works good for me and give the same result.

var str = '/assets/image';
if(str.indexOf('/assets') === 0){alert('done');}

Upvotes: 1

guest271314
guest271314

Reputation: 1

According to MDN, startsWith() is not supported at android. You can alternatively use RegExp.prototype.test()

var str = '/assets/image';
if(/^\/assets/.test(str)){alert('done');}

Upvotes: 1

Related Questions