Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a two-part CGI (CGI.pm) page: top is all headers and stuff, while the bottom is the result of a DB-query. I'd like to do my best to force the browser to render "top" part, even if the script isn't finished. I see that behaviour sometimes, but I don't know how to "force" it to happen (e.g. to force partial rendering). Any hints?

Replies are listed 'Best First'.
Re: Force Browser to Render Partial Page
by bobn (Chaplain) on Aug 28, 2003 at 02:54 UTC

    Be sure to unbuffer STDOUT with

    $|++;
    at the start of your script.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      When STDOUT is redirected to the browswer with CGI is it still buffered? I rarely see CGI scripts starting like that, which is why I ask.

        Consider this code:

        #!/usr/bin/perl -w $|++; use CGI; $q = new CGI; print $q->header(), $q->start_html(), "before wait<br /><br/>"; sleep 5; print "after wait\n\n\n", $q->end_html;

        As shown, my browser (Mozilla 1.4) renders "before wait", then several seconds later, "after wait" appears.

        If I comment out the $|++; then all of it shows up in the browser at the same time.

        --Bob Niederman, http://bob-n.com

        All code given here is UNTESTED unless otherwise stated.

Re: Force Browser to Render Partial Page
by jand (Friar) on Aug 28, 2003 at 04:10 UTC
    It depends on the browser. Mozilla normally starts to render data immediately. I think Opera is the same, but haven't tried it lately. Internet Explorer will wait until it has received 256 characters to do some automated MIME type detection (via FindMIMEFromData). Adding a couple of HTML comment lines to fill the buffer normally works to convince IE to start rendering immediately.
Re: Force Browser to Render Partial Page
by sgifford (Prior) on Aug 28, 2003 at 04:25 UTC

    Another thing is that browsers tend to wait until all tags which could affect size are closed to render an element. This is particularly important with tables, which often will not be rendered until every cell is received, along with the closing tags. Printing the data outside of a table will cause it to render as it comes in, at least in Mozilla and Netscape.

    It's possible that specifying the width and height of the enclosing element might make it render without waiting, for example giving a width and height attribute for a table, but I'm not sure about this.

    And of course, all of this is very browser dependent.

Re: Force Browser to Render Partial Page
by belg4mit (Prior) on Aug 28, 2003 at 03:39 UTC
    Not exactly a perl question ;-) In any event, FYI this behavior is highly browser specific as is use of the MIME-type x/multipart-mixed-replace.

    --
    I'm not belgian but I play one on TV.