in reply to [OT?] formatting response text for AJAX widget

The JavaScript code you are sending (as seen by using print $response) is:

$('override_...').innerHTML="<a href='#' onClick='javascript:makeForm( +"...", "...", "...");'>...</a>';

but it should be

$('override_...').innerHTML="<a href='#' onClick='javascript:makeForm( +\"...\", \"...\", \"...\");'>...</a>";

The first double-quote is wrongly closed with a single quote, and you have unescaped double-quotes.

Other potential problems:

Update: The following addresses quoting issues:

use strict; use warnings; use HTML::Entities qw( encode_entities ); sub js_from_text { for (@_ ? $_[0] : $_) { s/([\\"])/\\$1/g; return qq{"$_"}; } } sub html_from_text { for (@_ ? $_[0] : $_) { return encode_entities($_); } } my ($text_componentId, $text_componentType, $text_newTitle) = ...; my $js_component = js_from_text("override_$text_componentId"); my $js_componentId = js_from_text($text_componentId); my $js_componentType = js_from_text($text_componentType); my $js_newTitle = js_from_text($text_newTitle); my $js_onClick = "makeForm($js_componentId, $js_componentType, $ +js_newTitle);"; my $html_onClick = html_from_text($js_onClick); my $html_newTitle = html_from_text($text_newTitle); my $html = qq{<a href="#" onClick="javascript:$html_onClic +k">$html_newTitle</a>}; my $js_html = js_from_text($html); my $js_response = "\$($js_component).innerHTML = $js_html;"; print("$js_response\n");

There is a situation where hungarian notation is very useful. Making Wrong Code Look Wrong is a worthwhile read.

Replies are listed 'Best First'.
Re^2: formatting response text for AJAX widget
by Fletch (Bishop) on Aug 04, 2006 at 19:27 UTC

    The $('someId') is syntactic sugar provided by the Prototype library to fetch the DOM object for the element with that tag. If passed anything rather than a string it returns that object. More info here on the wiki.

Re^2: [OT?] formatting response text for AJAX widget
by geektron (Curate) on Aug 04, 2006 at 19:24 UTC
    I did just catch the first issue -- the improperly closed double quote. I switched the 'main' quoting back to  qq{} to ease up on the toothpick syndrome. Fixing that ... didn't help.

    the values i've been using for the three vars don't need escapes. i can tackle that issue once i have a working repsonseText replacer. they've been simple strings.

    $('overide_123').innerHTML is valid javascript. the same handler uses it for another (working) part of the application.

      Is it still the same error?
Re^2: formatting response text for AJAX widget
by geektron (Curate) on Aug 04, 2006 at 20:12 UTC
    I already had some initial entity encoding, but .... it seems the
    entity still interferes with the javascript quoting ....