http://qs321.pair.com?node_id=699390

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

Monks, I am writing a page using CGI::AJAX and I have run into some problems trying to all multiple events through an onclick action that are not part of the perl script itself. The following code works when I am not using CGI:AJAX:
<a href='javascript:void(0)' onclick=display(this)>Click Here</a>
However when I use CGI:AJAX when the page loads I am getting an 'Unterminated string constant error'. This is important because the javascript function I am trying to call, display, is local and not part of the perl function I am trying to reference. The final action I am attempting to complete is to retrieve a value from a sub in perl, load, and to execute the display javascript on the client side. Giving a final code similar to:
<a href='javascript:void(0)' onclick=load(['value'],['return_numbers'] +);display(this); />Click Here</a>
How do I call the display javascript function in a way that it does not attempt to refrence a sub in perl?

Replies are listed 'Best First'.
Re: CGI::AJAX onclick
by pc88mxer (Vicar) on Jul 22, 2008 at 21:43 UTC
    I think we need to see more of your code. CGI::Ajax shouldn't define the Javascript function display() unless you have told it to handle that function in your constructor.

    Update: I would also make sure that your JS attribute values have proper quotes around them, e.g. onclick="...". And, FWIW, this modification of the CGI::Ajax example program works for me - is this what you want to do?

      So what I am doing is creating a link that when clicked loads a perl function, display, then through the same click passes a variable to a sub in perl. What this is for is when a user clicks a title of a post it expands then will query a sql server to display the content under the expanded title. So, the code you posted I would have to remove the text attribute and make that a href then have an onclick event assocate with it to load display, and to pass the variable to exported_func
Re: CGI::AJAX onclick
by MidLifeXis (Monsignor) on Jul 22, 2008 at 21:56 UTC

    Hmm, where to start...

    • attributes should be quoted with double quotes, not single quotes: (<a href="...) or bare.
    • You have a /> before the Click Here</a>. Remove the / at the end of the opening a tag.

    These two items, with the code you are trying to parse, would give many parsers headaches. Clean up the output to the browser and see how it works.

    --MidLifeXis

Re: CGI::AJAX onclick
by Anonymous Monk on Jul 23, 2008 at 04:42 UTC
    Where is the error coming from? From the browser (if you have firefox, check error console) or the server (check the html source)?