JQuery.trim () IE browser compatibility issue

I tested the following in FF, OP, Chrome, Safari and IE. It works in them all except 3 IE that I tested: 8, 7 and 6.

// truncate testimonial var visiblePara = $('div.bannerUnder p.show'); if (visiblePara.text().trim().length > 150) { var text = visiblePara.text().trim(); var author = $('div.bannerUnder p.show > strong').text(); text = text.substr(0, 150) + "..."; visiblePara.text(text).append("<strong>" + author + "</strong>"); } 

It says:

The object does not support this property or method and points to this line:

 if (visiblePara.text().trim().length > 150) { 

What could be the problem?

+6
javascript jquery cross-browser internet-explorer
source share
2 answers

Try changing:

 visiblePara.text().trim().length 

in

 $.trim(visiblePara.text()).length 

You can even move the text variable up, with something like this:

 // truncate testimonial var visiblePara = $('div.bannerUnder p.show'); var text = $.trim(visiblePara.text()); if (text.length > 150) { var author = $('div.bannerUnder p.show > strong').text(); text = text.substr(0, 150) + "..."; visiblePara.text(text).append("<strong>" + author + "</strong>"); } 
+19
source share

trim is not a String.prototype method until IE 8. It already exists in other browsers for a while.

I tried this in IE8 and it worked for me. Use jQuery.trim () jQuery.trim(str) instead

+4
source share

All Articles