James
James

Reputation: 135

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?

Upvotes: 6

Views: 5815

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92324

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

Upvotes: 4

rsp
rsp

Reputation: 111506

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>");
}

Upvotes: 19

Related Questions