in reply to Re^2: send windowmessage from cgi back to form that called the cgi
in thread send windowmessage from cgi back to form that called the cgi

This is more java JavaScript related than perl, but i have a page where i want to execute java JavaScript when it loads. based on what it does so you could try

print $q->header( -type =>'text/html'); print <<"EODEOD"; <title>mytitle</title> <script language="JavaScript"> <!-- function iframeLoad(a) { window.parent.postMessage({ 'func': 'GetFileLoadStatus', 'message': 'Message text from iframe.' }, "*"); } //--> </script> <body onload="iframeLoad()"> EODEOD

Edit: a was just a temp var, my sub was

function iframeLoad(a) { a = document.getElementById("tableId").innerHTML; parent.document.getElementById("tableId").innerHTML = a; parent.document.getElementById("hiframe").src = "about:blank"; }
In the parent i had
... function showTable(loc) { document.getElementById("tableId").innerHTML="Waiting on:" + loc; document.getElementById("hiframe").src = loc; } ... <input type="button" value="run program" onclick="showTable('/cgi-bin/ +program.pl?parm1=1')"> <iframe id="hiframe" src="about:blank" style="visibility:hidden;displa +y:none"></iframe> <div id="tableId" style="background-color:lightgray"></div> ....
and the cgi call also returned
.... <div id="tableId"> <pre> some stuff to display more stuff </pre></div>

Replies are listed 'Best First'.
Re^4: send windowmessage from cgi back to form that called the cgi
by Mr. Muskrat (Canon) on Mar 14, 2017 at 16:21 UTC

    s/java/JavaScript/g

      s/java/JavaScript/g

      What does that mean?

Re^4: send windowmessage from cgi back to form that called the cgi
by tultalk (Monk) on Mar 14, 2017 at 18:07 UTC

    Hi:

    Don't want to do anything on load.

    Go here http://www.jala-mi.org/httpsdocs/jala_AdminCore.htm

    at bottom are two buttons. Click Get File Load Status to load the iframe with the results (nothing because the part did not run that produces results). The other button clears the iframe.

    I want to execute these funtions from the cgi script on the server. The first one loading results when the actual process is complete and the clear function from the close button on the response form in the iframe.

    Again the perl code (more of it) updating tables disabled

    if ($action eq "updatetable_3") { # warn("Entered update_tables.cgi with action = UpdateTable_3"); # UpdateUserTable($dbh); # $message = "Update User Data Table"; # $filemessage = "User Data Table Update Complete"; } elsif ($action eq "updatetable_4") { # warn("Entered update_tables.cgi with action = UpdateTable_4"); # UpdateTenantTable($dbh); # $message = "Update Tenant Data Table"; # $filemessage = "Tenant Data Table Update Complete"; } elsif ($action eq "updatetable_5") { # warn("Entered update_tables.cgi with action = UpdateTable_5"); # UpdatePropertyTable($dbh); # $message = "Update Property Data Table"; # $filemessage = "Property Data Table Update Complete"; } elsif ($action eq "updatetable_6") { # warn("Entered update_tables.cgi with action = UpdateTable_6"); # DownloadUserTable($dbh); # $message = "Download User Data Table"; # $filemessage = "Download of File Completed: '$count'"; } else { # print p (escapeHTML ("Impossible Error: selected action is unknow +n: $action")); $message = "Database Failure"; $filemessage = "File Load Falure. Impossible Error: selected action +is unknown: $action"; } warn("Just before close connection"); manageusers::CloseConnection(); #window.parent.postMessage({ # 'func': 'GetFileLoadStatus', # 'message': 'Message text from iframe.' #}, "*"); if ($action eq "updatereport"){ warn("In updatereport before CreateFataFeedbackForm"); CreateDataFeedbackForm($message, $filemessage); }

      I still think you do want to do things onload to get your alert box, but i did go to your page and now understand better what you want.

      ok for starters add this above "function onMessage(event)"

      function showTable(loc) { document.getElementById("tableId").innerHTML="Waiting on:" + loc; document.getElementById("dataDialog").src = loc; }
      then under
      <iframe id="dataDialog" name="dataDialog" style="float:right"; src=""; + border:1px solid black;" scrolling="no" allowtransparency="true" all +owfullscreen="false" frameborder="no" width="200" height="400" > </iframe>
      add
      <div id="tableId" style="background-color:lightgray"></div>
      now change
      <input type="button" class="jalaLtSans" value="Member Table Data Updat +e" style="background-color:#0D4A80; width : 250px; color:#fff" onClic +k = "self.location='https://www.jala-mi.org/httpsdocs/cgi-bin/update_ +tables-development.cgi?action=updatetable_3'"/>
      to
      <input type="button" class="jalaLtSans" value="Member Table Data Updat +e" style="background-color:#0D4A80; width : 250px; color:#fff" onClic +k = "showTable('https://www.jala-mi.org/httpsdocs/cgi-bin/update_tabl +es-development.cgi?action=updatetable_3')"/>
      .location and .src may do the same, my code uses .src

      Now try pressing the "Member Table Data Update" button, is that better?

      now if you still want the "Get File Load Status" button change its code to

      <input type="button" class="jalaLtSans" value="Get File Load Status" s +tyle="background-color:#0D4A80; width : 250px; color:#fff" onClick = +"showTable('https://www.jala-mi.org/httpsdocs/cgi-bin/update_tables-d +evelopment.cgi?action=updatereport')"/>
      How is that? aww but you want to get rid of the "waiting on" junk now dont ya? Well that is part of the side effect of my iframeLoad call from the cgi. so change your cgi-bins that use this technique to look something like
      print $q->header( -type =>'text/html'); print <<"EODEOD"; <title>mytitle</title> <script language="JavaScript"> <!-- function iframeLoad(a) { a = document.getElementById("tableId").innerHTML; parent.document.getElementById("tableId").innerHTML = a; parent.document.getElementById("dataDialog").src = "about:blank"; }//--> </script> <body onload="iframeLoad()"> <div id="tableId"> EODEOD ...... print '</div>';

      After running this a bit you may understand why my code has style="visibility:hidden;display:none" for hiframe

      now as an exercise for the reader, imagine that showTable instead looked like this

      function showTable(loc,table) { document.getElementById(table).innerHTML="Waiting on:" + loc; document.getElementById("dataDialog").src = loc; }
      and the onClicks were suitably modified to contain the proper ids, say table1,table2,table3 and these were spread out in your code
      <div id="tableid1" style="background-color:lightgray"></div> <div id="tableid2" style="background-color:lightgray"></div> <div id="tableid3" style="background-color:lightgray"></div>
      and each different cgi-bin program used its own tableid instead of the generic TableId i showed. cute huh? I just showed you stuff from the simpler page so you wouldnt get too confused. One side effect i havent corrected for is that only one "button" can be in progress at a time, i never put a locking protocol in place, i just dont press another button till the last has finished

      And yet i still tend to deny knowing any javascript,and Noscript blocks most pages i visit. I also deny knowing any cobol, c, or assembler too cuz last time i admitted to knowing cobol they made me program in it. and made me read other peoples dumps, Dont know TPF either

      BTW: Doesnt routine that send back html to the iframe? should have been "Doesnt the routine send back html to the iframe?" Aparently not, it just replaced the main page it seems.

      and Gad it takes me time to work out the bugs in this stuff.
      "what do you mean ME kemosabe"

      https://www.youtube.com/watch?v=oWuMevc6G4w edit: i like this one better https://www.youtube.com/watch?v=e1TmhBJxO70

        Ok. "and Gad it takes me time to work out the bugs in this stuff. "what do you mean ME kemosabe" "

        Us for a lesser portion. My problems have been massive.

        You say: "I still think you do want to do things onload to get your alert box"

        I still don't understand what you are implying.

        There is nothing to call to be put in the iframe on the parent page onload event. The database queries have not been run at that point so no results.

        Obvious thanks to you guys.

        Your comment "ok for starters add this above "function onMessage"

        There is no message event because I can't figure out how to send a message back to the form from perl.

        So I don't understand what you are trying to demonstrate. The calls through cgi to update the tables and return data sets work fine.

        They are all disabled so the calls will only return message with out actual execution of the database queries.

        As I started out, my fundamental problem is sending a wm to the form triggering a call to get the result message and show it in the iframe as demonstrated by clicking the test button.

        I hate things like polling but I can see no other way than starting a polling loop checking the value of a global variable set in the script when the original function call to do the database stuff finishes.

        When the variable (flag) is set the polling picks it up and calls the function to load the results into the iframe.

        I have looked at various "watch" examples that don't work and others that use a listener but all seem invoving sending a message to the window.

        Also looked at module for embedding javascript into perl but gave up.

        Simple polling loop started when original cgi command sent to script seems the way to go.

        I don't understand this statement: Aparently not, it just replaced the main page it seems.

        Are you saying that when you tested it live it just replaced the page with a new page and not put the result in the iframe

      Don't want to do anything on load.

      yes you sure do, you want to display a message when the cgi page loads that is sent back after you press the the Get File Load Status button

        But no message (results message) exists until one of the routines updating the database files is executed. After that the results are available for loading into the iframe.

        User clicks on button to do some processing. When a routine finishes, a window message is sent to calling form to get the results loaded into the iframe. There is nothing to load when the iframe is instantiated when the jala_AdminCore.htm is displayed. The buttons are there only for testing to make sure the functions perform as intended.

Re^4: send windowmessage from cgi back to form that called the cgi
by tultalk (Monk) on Mar 15, 2017 at 01:43 UTC

    Enough of this. I will ser a global variable as a flag and "watch" it in javascript on the html page. When it changes I request the form to load into the iframe.

      Following up on what huck suggested, here is a demo to play with using Ajax. Change the file paths and urls to suit your web server.
      An html page

      <html> <head><title>Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jque +ry.min.js"> </script> <script> $(document).ready(function() { $(".update").click(function(){ var button = this; $.ajax({ type: 'POST', url: 'runme.cgi', data: { 'action': $(button).attr("id") }, success: function(res) { alert( res.msg ); var myIFrame = document.getElementById("dataDialog"); myIFrame.src = "runme.cgi?action=result"; }, error: function() { alert("Error : did not work"); } }); }) }) </script> </head> <body> <iframe id="dataDialog" src="about:blank"></iframe> <h2>Using Ajax</h2> <button class="update" id="update_1">Update 1</button> <button class="update" id="update_2">Update 2</button> <button class="update" id="update_3">Update 3</button> <button class="update" id="update_4">Update 4</button> </body> </html>

      and the script runme.cgi

      #!/usr/bin/perl use CGI; use JSON; use strict; my $logtext; my $logfile = 'c:/temp/web/update.log'; # somewhere writeable my $q = new CGI; my $action = $q->param('action'); if ($action =~ /update/){ # do stuff to update database sleep 1; open OUT,'>', $logfile or die "$!"; print OUT "Result of $action on\n".scalar(localtime)."\n"; print OUT "Line $_ message\n" for (1..rand(10)); close OUT; my $json = encode_json( { msg => "$action done" } ); # response print $q->header( -type => 'application/json' ),$json; exit; } if ($action eq "result"){ # get results of last update if (-e $logfile){ open IN,'<', $logfile or die "$!"; $logtext = do {local $/;<IN>}; close IN; } print $q->header( {-pragma =>'no-cache', -cache_control => "no-store,no-cache,must-revalidate"} ), $q->start_html, $q->pre($logtext),$q->end_html; exit; }
      poj

      You should just loaded the cgi-bin call into the iframe rather than the main page in the first place.

        And indeed it is working fine as you suggested. Couldn't see the trees for the forest, so to speak. Thanks loads. Best regards