admin管理员组

文章数量:1417404

trim() is not working in Internet Explorer(it's working properly in Chrome and Firefox). However I need to trim the value to prevent empty spaces.

trim() is not working in Internet Explorer(it's working properly in Chrome and Firefox). However I need to trim the value to prevent empty spaces.

Share Improve this question edited Apr 24, 2012 at 10:45 user 87.1k18 gold badges199 silver badges190 bronze badges asked Apr 29, 2011 at 6:05 VissuVissu 2,0439 gold badges42 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I don't think JavaScript provides trim() does it? Some browsers may implement it, and various libraries implement it.

You can add your own, something like this:

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

If this turns out to be an IE bug, you can always implement your own workaround using RegEx in JavaScript like this left trim function sample - the sample can be expanded to trim both left and right sides. This Regex should work in all browsers.

Have a look @ this -> Trim not working in IE

本文标签: 39trim()39 javascript function issues with Internet ExplorerStack Overflow