Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Implement HTML5 progress bar with CGI program

Posted on 2013-12-04
3
Medium Priority
?
1,070 Views
Last Modified: 2014-02-01
I am trying to figure out how to use a HTML5 "progress" tag to implement
a progress bar in conjunction with a CGI host program.  The CGI is a compiled
C program which I can revise as necessary.  This program will take a while
and I want to have the user sit there looking at the progress bar.

So, in my html I can update the progress bar like this:

<script>
 var UpdateProgress = function(value) {
   var pbar = document.getElementById('p');
   pbar.value = value;
 }
</script>

<progress id='p' value='0' max='100'>
</progress>

So I need to call UpdateProgress() supplying whatever percentage the
host program is at.  My first thought was I would have the host program
output something like this:

print "content-type:text/plain\n\n"
print "25"
sleep 1
print "50"
sleep 1
print "75"
sleep 1
print "100"

and I would somehow intercept the data coming from the host and use
the value to call UpdateProgress().  But how do I intercept the data?
Anyway, nothing gets displayed in my browser until the host program
is done.

Sorry if this is dumb question but I am a newbie at this stuff.
0
Comment
Question by:jmckay321
  • 2
3 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39699241
If you initiate a request from the browser, then it is going just wait for the response.

To do what you want, you need to uses Ajax to initiate the server side process from an existing page that contains the progress element.

You actually have to use the progress event:
http://www.w3.org/TR/progress-events/

The bare bones script would look something like:
<script>
  var progressBar = document.getElementById("p"),
      client = new XMLHttpRequest()
  client.open("GET", "your.cgi")
  client.onprogress = function(pe) {
    if(pe.lengthComputable) {
      progressBar.max = pe.total
      progressBar.value = pe.loaded
    }
  }
  client.onloadend = function(pe) {
    progressBar.value = pe.loaded
  }
  client.send()
</script>

Open in new window



Cd&
0
 

Accepted Solution

by:
jmckay321 earned 0 total points
ID: 39699361
I think I've got this figured out.  Basically my host cgi program is emitting html, and I just need to put in that html the code:  <script>UpdateProgress(n)</script> every time I want to update the bar.  (And of course, create the UpdateProgress script before that).  It seems to be working.  The big problem I had was getting the browser to start accepting data from the cgi instead of waiting until the end.  I had to make it put out enough data to get the browser started, then fiddle with headers, and flushing the output buffer.  What a pain.
0
 

Author Closing Comment

by:jmckay321
ID: 39826131
Because it is correct.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question