Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .trim() IE Browser Compatibility Question

I tested the following in FF, OP, Chrome, Safari and IE. It works in them all except the 3 IEs 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:

Object doesn't support this property or method and points to this line:

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

What could be the issue?

like image 894
James Avatar asked Mar 11 '11 18:03

James


2 Answers

Try changing:

visiblePara.text().trim().length

to:

$.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>");
}
like image 115
rsp Avatar answered Sep 23 '22 04:09

rsp


trim is not a method of String.prototype until IE 8. It has existed in other browsers for a while now.

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

like image 25
Juan Mendes Avatar answered Sep 24 '22 04:09

Juan Mendes