1

I've got a browser-based app that downloads a bunch of stuff from a remote server, and I'm using the str_pad trick mentioned in this answer to push output incrementally. Something like this:

Downloading records for Property 1 into table rets_property_1
Query: (181=1980-03-01T00:00:00+),(18=1980-03-01T00:00:00+) Limit: 100
Selected fields: sysid,1,2,3,4,5,6,7,8,9,10,11,13,15,16,17,18
Fetching listings from server, please wait...
Number of listings returned: 100
. Downloading images
Images found: 2
Images downloaded: 2
. Downloading images
Images found: 1
Images downloaded: 1
. Downloading images
Images found: 4
Images downloaded: 4
... etc

I want to keep the browser scrolled to the bottom at all times, with the option to scroll back up, kind of like what you'd see using the command line.

Not sure if this is an obstacle, but there's no way I can think of to have closing tags on anything because of the way the output is being sent (the closing tags wouldn't be sent until after all the other output).

echo '<html><body>'; // first output sent
$obj->download_listings(); // takes a long time, sends output incrementally
echo '</body></html>'; // too late to be useful

Is there any way to make sure the newest piece of output is always displayed on the screen (which would be at the bottom after the screen is full)? I'm open to using even the hackiest of ideas using PHP, HTML, JS, or CSS.

Community
  • 1
  • 1
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228

2 Answers2

1
scrollTo(0, document.body.clientHeight);
Thomas Allen
  • 393
  • 1
  • 4
  • So how would I apply this in my case, which is not actually using javascript or `setInterval`, but output sent directly to the browser from the server? Send a ` – Wesley Murch Mar 30 '12 at 22:15
  • 1
    It's super-hacky, but since I am using a PHP function to send every piece of output, I just added `` to the function (whole script tag gets sent for every piece of output). Thanks for the inspiration, I can tighten this up on my own later, this is not a public facing app. – Wesley Murch Mar 30 '12 at 22:25
  • Hmmm... it seems to stop working after about 2-3 screens - not sure why. I'll have to work on this I guess. – Wesley Murch Mar 30 '12 at 22:30
  • Just as bash clears its own scroll-up over time, you may want to set a limit on this. I couldn't reproduce your issue here. I wouldn't send the script each time, makes more sense to include it in your interval function (or whatever your XHR success callback is). – Thomas Allen Mar 30 '12 at 22:33
  • The thing is I'm not using JS for the output. I think I threw a red-herring by posting that jsfiddle demo, it was just supposed to be a visual example. I think I need to listen to `body` changes with js, *then* scroll, instead of sending a ton of ` – Wesley Murch Mar 30 '12 at 22:35
  • ..now I'm not even sure if that makes sense. I'll pick this back up tomorrow morning and give you my thanks via upvote/checkmark if no other ideas are posted. Clearing the top of the "console" shouldn't be necessary, in fact I don't want to remove any output so I can review the entire thing when it completes. – Wesley Murch Mar 30 '12 at 23:05
1

If you position the <body> as {position:absolute;bottom:0} it's possible to mimic the command line output with new output at the bottom of the screen. But it simply scrolls off the top and isn't reachable once it's gone.

Here's your fiddle with that change to demonstrate.

Even using a <div> which must expand with its content, I can't get a scroll bar. But it must be possible: perhaps this will be a start.

Andrew Leach
  • 12,945
  • 1
  • 40
  • 47
  • Hey great idea, this is brilliant! Yes the scroll bar thing problem is relevant, but this is a very nice easy way to tackle the important part. I added `html:hover body{position:relative}` and that's good enough for me: http://jsfiddle.net/rGMKe/3/ – Wesley Murch Mar 31 '12 at 02:30