JavaScript cannot be solved with the trim function in IE

Source: Internet
Author: User

The trim function of JavaScript is not a problem with Firefox

JS Code
    1. <script language="JavaScript" >
    2. var test1 = "AA";
    3. Test1 = Test1.tostring ();
    4. Test1 = Test1.trim ();
    5. </script>

It's not a problem to use under Firefox, but the error is under IE
Then we can modify it.

JS Code
    1. string.prototype.trim=function () {return this.replace (/(^\s*) | ( \s*$)/g,"");}

Add this sentence to your head, which can be run under IE and ff.

JS Code
    1. <script language="JavaScript" >
    2. string.prototype.trim=function () {return this.replace (/(^\s*) | ( \s*$)/g,"");}
    3. var test1 = "AA";
    4. Test1 = Test1.tostring ();
    5. Test1 = Test1.trim ();
    6. </script>

The method provided by jquery:

HTML code
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <script src="Http://code.jquery.com/jquery-latest.js"></script>
  5. </head>
  6. <body>
  7. <button>show Trim Example</button>
  8. <script>
  9. $ ("button"). Click (function () {
  10. var str = "Lots of spaces before and after";
  11. Alert ("'" + str + "'");
  12. str = Jquery.trim (str);
  13. Alert ("'" + str + "'-no longer");
  14. });
  15. </Script>
  16. </body>
  17. </html>

JavaScript cannot be solved with the trim function in IE

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.