
Zhris
User
Jul 23, 2010, 1:22 PM
Post #2 of 2
(1144 views)
|
|
Re: [brsaran] is there any way to clear the HTML output printed by CGI perl
[In reply to]
|
Can't Post
|
|
Hey, When you refer to "download", I nearly assume that you are physically downloading a file through your browser download dialog. If I am right in saying, you are "reading" data from a database, and whilst the process takes place you would like to dynamically output its progress. Using CGI alone, this wouldn't be possible (theres no way to clear the screen, without "re-opening" the page). However, although i've never tried, your task would be possible with a client-side language (javascript). Non-dynamically, you would print output like the following:
#i.e. in a loop print "$message\n"; #Outputs: for ex: "1/30 downloading" for ex: "2/30 downloading" for ex: "3/30 downloading" etc Dynamically, you would print a block of javascript with code inside that calls a function elsewhere, passing perl variable:
#i.e. in a loop print "<script type=\"text/javascript\">progress('$message');</script>\n"; #Outputs (last loop iteration): for ex: "30/30 downloading" Although it may look as if its only outputted 1 line, its actually outputted the javascript block multiple times, but is only visible in the source code. Anyway, using this theory, I wrote a quick/basic example of how its done. I tested in Firefox 3.6.6 and Internet Explorer 8 only:
#! /usr/bin/perl use strict; use CGI ':standard'; use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; print "Content-type: text/html\n\n"; ########## print qq( <html> <head> <title>CGI Dynamic Progress</title> <script type="text/javascript"> function Progress(message) { var e; if (document.getElementById) { e = document.getElementById("progressdiv"); } else if (document.all) { e = document.all["progressdiv"]; } else if (document.layers) { e = document.layers["progressdiv"]; } e.innerHTML = message; } </script> </head> <body> <div id="progressdiv"></div> ); ########## for (1 .. 1000) { my $message = ($_ < 1000) ? "Loop iteration: $_" : "Finished"; print "<script type=\"text/javascript\">Progress('$message');</script>\n"; } ########## print qq( </body> </html> ); Hope this helps, Chris Edit: I found a very similar script. I have edited the javascript to ensure its more browser compliant.
(This post was edited by Zhris on Jul 23, 2010, 4:11 PM)
|