Prototype.js Ajax.Request: Beware of carriage returns in response text

Another in the long running saga "Dumb things Rob did with Javascript." I ran across a strange error in some fairly simple Ajax code the other day. Here's the code:

function () {new Ajax.Request(urlTxtSrch+'&txtTEXTSRCH=' + encodeURIComponent( $('txtTEXTSRCH').value),
catch(ex) {$('textSearchResults').innerHTML = '<p>Error getting results: ' + ex.message + '</p>'}},
asynchronous:true}); return false;}

Using prototype, I'm taking the value out of a text box and returning some javascript code which writes a list of search results into a div. Not the best way to do it, but it was basically a one off situation so there wasn't much benefit to making it more generic and figuring out how to do without the nasty eval. The search results were limited to twenty items with three bits of info each, the final one of which was the first 200 characters of a 4000 character varchar field we have in our database.

The error I kept getting was malformed string. It didn't happen all the time, in fact not very often at all, so it took me a while to work out that it was particular result items which were causing the problem rather than simply the size of the results. The problem, as many will no doubt already be aware, is that string literals in javascript cannot extend across multiple lines. In fact, I too was aware of this, and my server side code was already replacing vbCrLf (yes, it's VB running on Windows) with <br> so I was a little confused as to why it was happening.

Some people have probably already spotted the dumb assumption I was making. Since I was already truncating the 4000 character field to 200 characters there were some situations where the 200th character was the carriage return part of a carriage return plus line feed combo (the required line ending on Windows). Javascript is quite happy to accept a carriage return by itself as the end of a line where Windows is not.