The startsWith function in js cannot be compatible with any browser

Source: Internet
Author: User
The following is a small article about the incompatibility of startsWith functions in js in any browser. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the startsWith function used for js testing. However, it is not available in every browser, So we generally need to rewrite this function, the specific usage can be summarized.

In some browsers, he is undefined, so we can handle it like this,

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

This should be placed in the function to be loaded on the page, otherwise it will be difficult.

There is also a direct rewrite method, but I haven't tested it yet. You can test it:

String.prototype.startWith=function(str){  if(str==null||str==""||this.length==0||str.length>this.length)   return false;  if(this.substr(0,str.length)==str)    return true;  else    return false;  return true; }

Some say that there are no startsWith and endWith functions in js, but even if some browsers are not declared, they can still be used, but we still want to rewrite them for compatibility.

if (typeof String.prototype.endsWith != 'function') { String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; };}

Use regular expressions to implement startWith and endWith Functions

String. prototype. startWith = function (str) {var reg = new RegExp ("^" + str); return reg. test (this);} // test OK, use str directly. endWith ("abc") can be called as String. prototype. endWith = function (str) {var reg = new RegExp (str + "$"); return reg. test (this );}

In the above js article, the problem that startsWith functions cannot be compatible with any browser is all the content shared by xiaobian. I hope to give you a reference and support for PHP.

For more information about the incompatibility of startsWith functions in js with any browser, please refer to PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.