I have a one line function that works great in Chrome, and FF to shorten a string but throws an exception in IE. Does anyone know of a workaround for IE?
Here is my code:
var name = 'This is a really loooooooooooong string';
var shortName = name.trim().substring(0, 10).split(" ").slice(0, -1).join(" ") + "...";
alert(shortName );
Thanks,
-Paul
trimwas standardized in ECMAScript 262 5th Edition, so it should be available in IE 9. For IE 8 and older, there are a couple of solutions available to you.Since you tagged jQuery, you can use
$.trim(str):Alternatively, you can implement
String#trimusing a shim, like this one:There’s also a decent comparison on different
trimimplementations available at Steven Levithan’s blog.