IE7/8 Error: Object does not support "trim" property or method

Source: Internet
Author: User
Tags rtrim trim

Workaround:

Method 1:

Use the global function in jquery $.trim () instead of the native JS method Trim (): $.trim ($ ("input[type= ' text ']:eq (0)"). Val (). Trim ());

Method 2:

To extend the string method using native JS

1. Methods written as class: [Calling Format: Str.trim (); ]

The code is as follows Copy Code

<script type= "Text/javascript" >

Function.prototype.method = function (name, func) {

This.prototype[name] = func;

return this;

};

if (String.prototype.trim) {//whether the browser has its own trim () method

String.method (' Trim ', function () {

Return This.replace (/^s+|s+$/g, "");

});

String.method (' LTrim ', function () {

Return This.replace (/^s+/g, "");

});

String.method (' RTrim ', function () {

Return This.replace (/s+$/g, "");

});

</script>


Example

The code is as follows Copy Code

<input id= "Demo" type= "value=" has space "/> <a href=" javascript:; "onclick=" Test (); " > Click to remove Space </a> <script type= "Text/javascript" >
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
if (! String.prototype.trim) {//Determine if the browser has its own trim () method
String.method (' Trim ', function () {
Return This.replace (/^s+|s+$/g, "");
});
String.method (' LTrim ', function () {
Return This.replace (/^s+/g, "");
});
String.method (' RTrim ', function () {
Return This.replace (/s+$/g, "");
});
}
Test Call Method: Trim ()
document.getElementById ("Demo"). Select ();
var Str=document.getelementbyid ("Demo"). Value;
function Test () {
document.getElementById ("Demo"). Value=str.trim (); Can be replaced with Str.ltrim () or Str.rtrim ()
document.getElementById ("Demo"). Select ();
}
</script>

Workaround Two

Write function: [Call Format: Trim (str)]

The code is as follows Copy Code

<script type= "Text/javascript" >

function Trim (str) {//Remove spaces at left and right ends

Return Str.replace (/(^s*) | ( s*$)/g, "");

}

function LTrim (str) {//delete left space

Return Str.replace (/(^s*)/g, "");

}

function RTrim (str) {//Remove the right space

Return Str.replace (/(s*$)/g, "");

}

</script>

Instance

The code is as follows Copy Code

 <input id= "demo" type= "value="       have space          "/> <a href=" javascript:; "onclick=" Test (); " > Click to remove Space </a> <script type= "text/javascript"
  function trim (str) {//remove spaces on both sides
       return Str.replace (/(^s*) | ( s*$)/g, "");   
}
Function LTrim (str) {//delete left space
     return str.replace (/(^s*)/g, "");
  
function RTrim (str) {//delete the right space
     return str.replace (/(s*$)/g, "");

 //Test Invocation method: Trim ()
 document.getelementbyid ("Demo"). Select ();
 var str= document.getElementById ("Demo"). Value;
 function Test () {
 document.getelementbyid ("Demo"). Value=trim (str);//Can be replaced by Str.ltrim () or Str.rtrim (
 document.getelementbyid ("Demo"). Select ();
 }
</script>

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.