Solved

Implement HTML5 progress bar with CGI program

Posted on 2013-12-04
3
883 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now