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

When I try to call an Ajax function from a Javascript-generated pop up, it won't work..
# Perl CGI script: use myPackage; ### CGI Object ### my $cgi = new CGI; ### Ajax Object ### my $pjx = new CGI::Ajax( 'exported_func1' => \&exported_func1_code, 'exported_func2' => \&exported_func2_code, ); print $pjx->build_html($cgi, \&Show_HTML); sub exported_func1_code{ my $commentsobj = new myPackage(); ## some code that works.. (not package problem) ## this returns the data to "showContextMenu" ## and I can actually see the pop up.. $someHTML = " <!-- some HTML --> <input type=\"BUTTON\" id=\"SubmitId\" name=\"SubmitId\" VALUE +=\"Submit\" onClick=\"exported_func2(['CommentValue'],['dummyName']); +\"><center> <!-- some HTML --> "; return $someHTML; } sub exported_func2_code{ ## this one is called from inside the popupMenu ## but seems it cant be called from there..any suggestion? my ($p1,$p2,$p3,$p4) = split("\_",shift); my $commentsobj = new myPackage(); my $result = $commentsobj->myFunction($p1,$p2,$p3,$p4); return 1; } sub Show_HTML{ my $html_all = " <!-- headers.. --> <script type=\"text/javascript\"> function showContextMenu(input) { var menuContent = ''; menuContent = '<table width=100\%><tr><td>'+ input +'</td></tr +></table>'; var menuWidth = 400; var menuHeight = 400; var menuLeft = 200; var menuTop = 200; var popupMenu = parent.window.createPopup(); var popupMenuBody = popupMenu.document.body; popupMenuBody.style.border = 'solid black 1px'; popupMenuBody.style.fontSize = '2pt'; popupMenuBody.style.fontFamily = 'Arial'; popupMenuBody.innerHTML = menuContent; for (var i = 0; i < popupMenuBody.all.length; i++) { if(popupMenuBody.all[i].id == \"TextAreaId\"){ popupMenuBody.all[i].onkeypress = updateText; } } popupMenu.show(menuLeft, menuTop, menuWidth, menuHeight, docum +ent.body); return false; } function updateText(){ //var keypressedCode = e.keyCode || 1; //this does not work ! //var inputtext = String.fromCharCode(keypressedCode); //neith +er does this work ! this.value += 'HI'; } </scrip> <!-- more html.. --> <td onmouseover=\"exported_func1(['mySourceDivID'],[showCo +ntextMenu]);\">SomeText</td> <!-- more html.. --> "; return $html_all; }
The problem is that the code is able to call the "exported_func1_code" from the main page, but I'm not able to call the "exported_func2_code" from inside the pop up, any ideas? (it just tells me the script stopped running) Thanks ! EDIT: fixed code for exported_func2_code, sorry for the typo

Replies are listed 'Best First'.
Re: Ajax function from js pop up
by Anonymous Monk on Nov 01, 2012 at 00:29 UTC

    ut I'm not able to call the "exported_func2_code" from inside the pop up, any ideas?

    Where do you try to call exported_func2?

      Sorry for the typo , just fixed it. Although I ended up taking the code from exported_func2_code and putting it in a separate perl script which I redirect from the button's onClick function .. although I'd like to know what the issue is by using perl ajax.
      <input type="Button" onClick="window.open('exported_func2.pl?param1=$var');">

        although I'd like to know what the issue is by using perl ajax.

        What does that mean?

        One issue I see is about style/maintainability, using "\"" where you could have saved yourself considerable typing effort by using alternate delimiter qq{"} , or using a template

        The more important issue, is you're trying to debug html/javascript inlined in a perl program -- get rid of all non-html non-javascript parts, get a copy of Firebug, and figure out the html/js separately before inserting it someplace else

        Also, trying to debug javascript through perlmonks isn't likely to succeed