Christian Heilmann

You are currently browsing the Christian Heilmann blog archives for August, 2006.

Archive for August, 2006

On vacation now

Friday, August 25th, 2006

I am now on vacation till next Friday to go to Paris. No comments will make it through, but I promise to answer what accumulates.

Shortening strings to a fixed length in JavaScript

Friday, August 25th, 2006

I have this function I’ve used for quite a long time to shorten a string to a fixed amount of characters without breaking in between words:

function constrain(str,n){
var words = str.split(’ ‘);
var resultstr = ‘’;
for(var i = 0; i < words.length; i++ ){
if((resultstr + words[i] + ’ ‘).length>=n){
resultstr += ‘…’;

resultstr += words[i] + ’ ‘;

return resultstr;

ately that it can get really slow with massive strings in Internet Explorer. This is a well-known issue and the solution is to use arrays instead of concatenating strings.

This made me realise how convoluted the above example is and that it is much easier to do the same with an array:

function constrain(str,n){
if(str.length > n){
var s = str.substr(0, n);
var words = s.split(’ ‘);
words[words.length-1] = ‘’;
str = words.join(’ ‘) + ‘…’

return str;

You send a string and the amount of characters wanted as parameters and the function returns the string with the right lenght followed by a “...”.

Another way to work around the MSIE issue of slow concatenation is to use thea string builder.

More newsagent fame and a devastating review

Tuesday, August 22nd, 2006

I guess you have to take the good with the bad…

This morning I found out that there is a new issue of NET Magazine with an adequately loud mouthed orange man on the cover that features my opinion piece where I am Captain Obvious to the rescue and talk about the biggest trick in successful web development. I guess my free copy is in the mail…

My elevation of seeing my mugshot that big (No, it is not vanity that made me happy but knowing I can send this to my family and they’ll be proud as punch although they don’t know English or anything about computers) got a big dent though when I checked My book’s page on and found a new devastating review adding to the very positive one that’s been there for ages. I am really not sure how to react to something like that as I hate disappointing people and cannot really understand what that reviewer was on about. It is not as if there wasn’t enough material on the web to check the book before you buy it, and I spent a long time making sure that the web site for the book has all the code neatly sorted and easy for you to try out. You just cannot please anybody I guess, and I hope that not too many people get discouraged from giving the book a try. Writing a JavaScript book for beginners that does cover Ajax was a balancing act, I guess I’ll have to expect more of that.

Drew made me do it: Cover all your CSS class tasks with one small JavaScript

Monday, August 21st, 2006

Drew McLellan asked for a tool script for an upcoming project that would allow him to do all kind of tasks related to CSS classes.
He wanted to dynamically add, remove and check for classes and get all elements that have a certain class applied to them.

It is nothing fancy or new, but come in handy for you, too: Check the CSS class scanner tool

Unobtrusive Goodness for Ruby on Rails

Monday, August 21st, 2006

Dan Webb and Luke Redpath have released an Unobtrusive JavaScript for Rails Plugin. Well done! Now let’s do the same for Visual Studio and .NET!