rashley has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks. I have the following CGI code to create a button that calls a Javascript.

$result .= $cgi->button( -name=>'AddFlagNote', -value=>'Add Flag Note', -onclick=>qq{return addNote(\'FlagNotesTable\');});

The problem is that last line. The HTML I get is

onclick="return addNote('FlagNotesTable');"

when what I want is

onclick="return addNote('FlagNotesTable');"

I've tried all of the following with no luck:

qq{return addNote(\'FlagNotesTable\');}); q{return addNote(\'FlagNotesTable\');}); qq{return addNote('FlagNotesTable');}); q{return addNote('FlagNotesTable');}); "return addNote(\'FlagNotesTable\');"); 'return addNote(\'FlagNotesTable\');'); "return addNote('FlagNotesTable');"); 'return addNote('FlagNotesTable');');

How do I get the single quotes in there? Thanks.

Replies are listed 'Best First'.
Re: CGI, Javascript and Single Quotes
by ikegami (Patriarch) on Nov 15, 2006 at 16:22 UTC

    onclick="return addNote('FlagNotesTable');"
    and
    onclick="return addNote('FlagNotesTable');"
    are the same thing. Both will execute
    return addNote('FlagNotesTable');
    JavaScript will never even see '. It will be transformed to ' before being passed to JavaScript.

      This doesn't seem to be the case for me.

      I tried inserting ' in place of ' on my little test HTML page, and the Javascript bombed.

        Your browser is broken if this is a problem. According to the strict DTD, what's in between script tags and event handlers is to be considered CDATA, which is defined as "a sequence of characters from the document character set and may include character entities." ' is a character entity.

        IE6 and FF1.5 had no problems handling entities in the event handler when I tested them using the following HTML doc:

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <title>test</title> <p><a onclick="alert(&#39;FlagNotesTable&#39;);">escaped</a> <br><a onclick="alert('FlagNotesTable');">Not escaped</a>

        Update: Added first paragraph.

Re: CGI, Javascript and Single Quotes
by Melly (Chaplain) on Nov 15, 2006 at 16:32 UTC

    You can use autoEscape(0) to turn off escaping and autoEscape(1) to turn it back on - but it doesn't look like a very good idea IMHO - particularly as the escaping doesn't hurt you.

    e.g.

    autoEscape(0); $result .= $cgi->button( -name=>'AddFlagNote', -value=>'Add Flag Note', -onclick=>"{return addNote('FlagNotesTable');}"); autoEscape(1);
    Tom Melly, tom@tomandlu.co.uk
Re: CGI, Javascript and Single Quotes
by ptum (Priest) on Nov 15, 2006 at 16:14 UTC

    Hmmmm. I don't see the problem ... the following sample code seems to work for me:

    #!/usr/local/bin/perl use strict; use warnings; use CGI qw/:standard :html3/; print header; my $test_button = button(-name=>'Test',-value=>'Test Val',-onClick=>"r +eturn test('Testing!');"); print start_html(); print <<JAVASCRIPT; <script> function test (message) { alert(message); } </script> JAVASCRIPT print startform(); print $test_button; print endform(); print end_html();
      I tried this. The only difference is that instead of prints I used  result .= $test_button;

      I still get the incorrect results.

        It seems likely to me that there is some simple problem that you (and we) are overlooking. Please post a concise (yet complete) example of your code and clearly identify the result that you are seeing (and what you want to happen).

        If this problem strays very far into Javascript issues, you may be better off getting help elsewhere, although many monks do know quite a bit about Javascript. While we're on that topic, what browser (version) are you using to run your code, and on what platform?

Re: CGI, Javascript and Single Quotes
by rashley (Scribe) on Nov 15, 2006 at 21:42 UTC
    Turns out there was a bug in the Javascript. My J/S developer is grovelling for forgiveness as we speak.

    Thanks for all the help, guys!