Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trim() function doesn't work in IE8?

Whenever I use the trim() function on a string, it works fine with Chrome and Firefox but I get an error in IE8 saying :

Object doesn't support this property or method

Can anyone tell me why this happens and if there is a work around?

like image 530
Mukul Avatar asked Jun 27 '12 05:06

Mukul


2 Answers

IE8 doesn't support the trim function. Here's a polyfill:

if(typeof String.prototype.trim !== 'function') {   String.prototype.trim = function() {     return this.replace(/^\s+|\s+$/g, '');    }; } 
like image 162
nemo Avatar answered Oct 18 '22 02:10

nemo


if you want you can add jquery and use $.trim(....) this will work..

$.trim("  hello "); 

give you

"hello" 
like image 32
Karesh A Avatar answered Oct 18 '22 02:10

Karesh A