Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

CGI progress information using Javascript

by Celada (Monk)
on Dec 06, 2005 at 20:01 UTC ( [id://514624]=CUFP: print w/replies, xml ) Need Help??

This note Re: Creating a "Progress" page with CGI reminded me of something I did about a year ago in a CGI to show status information to the user while a CGI was generating output. The CGI can generate output as it goes along which appears in the main body of the browser window and mixed in with this, update the user about the stages it is going through by chaning the text in a status line at the top.
# # If Javascript is disabled in the browser, the effect # should be that no status line will appear, but # everything will still work. This is important! # # Call makestatusline near the start of execution, just # after outputting the HTTP header or soon after so that # the status line will appear ner the top of the page # sub makestatusline { print <<"EOT" ; <script language="JavaScript1.2"><!-- if (document.getElementById || document.all || document.layers) document.write("<div id=statusbox><p>Status: </p></div>"); function newstatus(s) { var e; if (document.getElementById) { e = document.getElementById("statusbox"); } else if (document.all) { e = document.all["statusbox"]; } else if (document.layers) { e = document.layers["statusbox"]; } e.innerHTML = "<p>Status: " + s + "</p>"; } //--></script> EOT } # # Now call this function once in a while when you have # something to say to the user. Just make sure you don't # call it while you are in the middle of outputting an # HTML tag (finish the tag first, then call it) # # INPUT: a string, to display in the status line sub newstatus { my ($s) = @_; $| = 1; print <<"EOT" ; <script language="JavaScript1.2"><!-- newstatus("$s"); //--></script> EOT }

Replies are listed 'Best First'.
Re: CGI progress information using Javascript
by TedPride (Priest) on Dec 07, 2005 at 10:36 UTC
    Hmm, I made something like this, only using a block of images and a function to switch them "on" according to the current progress. Problem is, while NS seemed to work fine at doing line-by-line interpretation of my Javascript routine calls, IE tended to store them up, and the result was that the progress only got updated every 5th call or so. Does the same thing happen here?
Re: CGI progress information using Javascript
by Anonymous Monk on Jul 09, 2006 at 16:04 UTC
    I can't get it to work properly under mod_perl. I think the problem is related to the flush buffer $|=1. Any suggestions are appreciated...
      OK, I got it to work sort of under mod_perl2 but I get mixed results. Sometimes it works, sometimes I get segmentation faults, and sometimes I get the following error in my Apache error_log which I don't understand:

      "$r->rflush can't be called before the response phase at /html/perl/test.pl line 107"

      The complete code is as follows:
      #!/usr/bin/perl my $r = shift; use CGI; local our $cgi = new CGI; our $action = ""; $action = $cgi->param("action") || 'test_code'; if ( $action eq "test_code" ) { test_code (); } elsif ( $action eq "run" ) { test_prog ($cgi); } sub test_code { # my ($cgi_sub) = @_; my $cgi_sub = new CGI; print $cgi_sub->header(); print $cgi_sub->start_html( -title => 'Javascript calls from Perl' + ); print $cgi_sub->h2("test code"); print $cgi_sub->div($cgi_sub->font({-face=>'Arial'},"Status: " +)); print $cgi_sub->start_form(); print $cgi_sub->submit(-name=>'action',-value=>'run'); print $cgi_sub->end_form(); print $cgi_sub->end_html(); } sub test_prog { my ($cgi_sub) = @_; print $cgi_sub->header(); print $cgi_sub->start_html( -title => 'Javascript calls from mod_p +erl' ); print $cgi_sub->h2("test code"); print <<"EOT" ; <script language="JavaScript1.2"><!-- if (document.getElementById || document.all || document.layers) document.write("<div id=statusbox><font face=Arial>Status: </font></div>"); function newstatus(s) { var e; if (document.getElementById) { e = document.getElementById("statusbox"); } else if (document.all) { e = document.all["statusbox"]; } else if (document.layers) { e = document.layers["statusbox"]; } e.innerHTML = "<font face=arial>Status: </font><font face=arial color=blue><b>" + s + "</b></font>"; } //--></script> EOT for (my $i = 1; $i <= 6; $i++){ if ($i == 6){ newstatus("Aggregating Data..."); sleep (2); newstatus("Analyzing Data..."); sleep (2); newstatus("Finished \(this text is from the server\)"); } else { newstatus("Querying " . "$i" . " out of 5 suppliers"); sleep (1); } } print $cgi_sub->start_form(); print $cgi_sub->submit(-name=>'action',-value=>'run'); print $cgi_sub->end_form(); print $cgi_sub->end_html(); } sub newstatus { my ($s) = @_; print <<"EOT" ; <script language="JavaScript1.2"><!-- newstatus("$s"); //--></script> EOT $r->rflush; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://514624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found