Sunday, February 17, 2013

java script- trim function problem in ie8 solution

Problem: Java Script trim() function doesn't work in IE8 browser.

Error Message: Object doesn't support this property or method 

As you know, whenever you use trim() function of java script, it works fine with Chrome and FireFox browsers but you get an error with Internet Explorer (IE8).


$(document).ready(function () {

if (typeof String.prototype.trim !== 'function') { 

String.prototype.trim = function () {

return this.replace(/^\s+|\s+$/g, '');




If you want, you can add jquery and use $.trim(....) this will work fine

$.trim(" hello ");

gives you


Powered by Code Imagine