Hello Monks,

In my program several windows are selectively opened to display files for reading and/or modifying. When a window is opened or focused (if already opened) directly via a form button that calls a javascript window function, there is no problem.

However, when generating a tag passed by a form button to a textfield, which in turn signals a call from a subroutine to the same javascript window function, a second window will open, but if opened, it will not focus. A sample program(below) with 'alerts' inserted in the functions shows that the call from the subroutine does reach the js window function.

My question:
Is the script used in the subroutine a good and efficient way to call a js function? If so, what is the nature of (what is missing in) the call from the subroutine as opposed to that of the direct(win1) call to the js window function such that, while reaching the second nested 'if' block of this function which opens a window(win2), it can't as well focus the window via statements that are parallel to those in the first nested 'if' block?

#!/usr/bin/perl -w use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use strict; my $query = new CGI; my $JSCRIPT=<<EOF; function sub_dispatcher(passit) { alert("This \'Alert\' is within the js sub_dispatcher fuction which +holds the argument \'" + passit + "\' passed to it from the (indire +ct) form button. This value \'" + passit + "\' is in turn passed to a + textfield."); document.sql_tool.tag.value = passit; document.sql_tool.submit(); } var url1 = 'http://bioinfo.weizmann.ac.il/safety/images/db_connect_qfi +le.htm'; var url2 = 'http://bioinfo.weizmann.ac.il/safety/images/db_connect_qfi +le.txt'; var myRef1; var myRef2; function windo(arg){ if(arg == "win1"){ if(myRef1 == null || myRef1.closed){ myRef1 = window.open(url1,'mywin1','left=20,top=20,width=500,h +eight=500,resizable=1,scrollbars=1'); }else if(!myRef1.closed){ myRef1.focus(); myRef1.location.reload(true); } }else{ alert('Now inside js window function. Statements that follow ope +n win2, but will not focus window if already open. \(What is lost her +e that is not lost with the other \(direct\) form button \(win1\) whi +ch applies parallel statements?\)'); if(myRef2 == null || myRef2.closed){ myRef2 = window.open(url2,'mywin2','left=20,top=20,width=500,h +eight=500,resizable=1,scrollbars=1'); alert("Inside inner \'if\' block; opening win2"); }else if(!myRef2.closed){ alert("focus win2"); myRef2.focus(); myRef2.location.reload(true); } //resetting document.sql_tool.tag.value = 'waiting to receive tag'; document.sql_tool.submit(); } } EOF ; #SETTING HEADER - HTML: print $query->header(); print $query->start_html(-title => 'Selection ', -BGCOLOR=>'lavender', -script=>$JSCRIPT); #SETTING UP A FORM FOR INPUT: print $query->startform(-name=>'sql_tool'); print $query->button(-name=>'listfile', -value=>'direct function-call', -onClick=>"windo('win1')" ),p; print $query->button(-name=>'embedvar', -value=>'indirect function-call', -onClick=>"sub_dispatcher('call')" ),p; print $query->textfield(-name=>'tag' #-value=>'passed' ),p; print $query->endform(), hr; #------------------------------------------------------ #TAG VARIABLE: my $sub_select = $query->param('tag'); print "Now $sub_select from textfield",p; my $comment = "The tag \'$sub_select\' read true in a control statem +ent so that a subroutine is called in which, in turn, the js window f +unction is called by argument."; #JS FUNCTION CALL: if($sub_select eq 'call'){ print "$comment",p; viewfile(\$comment); } print end_html; sub viewfile() { my $writsql= "-code=>\'windo(\"win2\")\'}),p"; my $prntvar = "print \$query->start_html(-script=>{-language= +>\'JavaScript',$writsql;\n"; eval $prntvar; }

Simply, seeking guidance on how to call a js function from within a Perl program.
Thank you.


In reply to Problem calling a javascript function from a Perl subroutine by lev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.