30/08/07

Permalink 10:12:46 pm, by robertc Email , 341 words, 636 views   English (UK)
Categories: Front End Web Development Share on reddit Share on digg Share on del.icio.us Share on ma.gnolia.com Share on StumbleUpon

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),
{onComplete:function(request){try{eval(request.responseText)}
catch(ex) {$('textSearchResults').innerHTML = '<p>Error getting results: ' + ex.message + '</p>'}},
evalScripts:true,
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.

Technorati tags for this post:

Trackback address for this post:

http://www.boogdesign.com/b2evo/th1srv/trackback.php/209

Comments, Trackbacks, Pingbacks:

No Comments/Trackbacks/Pingbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

Hot Topics

    Popular

    boogdesign posts

    Longer posts on standards based web design, portable web development and Linux, intermingled with some stuff on my other nerd interests.

    Search

    Syndicate this blog XML

    • RSS 0.92 Posts RSS 0.92 Comments
    • RSS 1.0 Posts RSS 1.0 Comments
    • RSS 2.0 Posts RSS 2.0 Comments
    • Atom Posts Atom Comments
    What is RSS?

    License info

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-Share Alike 2.5 License.

    powered by
    b2evolution

    get hCard