views:

1391

answers:

7

Google chrome doesn't behave the same as other browsers when encountering this nugget:

<?php
while (true) {
    echo "<script type='text/javascript'>\n";
    echo "alert('hello');\n";
    echo "</script>";
    flush();

    sleep(5);
}
?>

It seems that it's waiting for the connection to terminate before doing anything.

Other than polling how can I do a similar thing in Google Chrome?

+1  A: 

I wish I had access to Chrome at the moment to test out some ideas. Have you tried adding some HTML after </script> and seeing if it renders incrementally? I imagine it would, and if so that'd be proof that Chrome doesn't want to run javascript in <script> elements while the page is loading. Of course, rendering the markup might trigger your scripts to run. If not, you could try including the javascript as external files and see if that affects execution time.

I think browsers generally have some leeway according to the spec in when they begin executing javascript, especially as the page loads. It might not be possible to do this in a fully cross-browser way without polling.

Peter Burns
+1  A: 

Some browsers require a certain number of bytes to be downloaded before rendering available data. I remember the last time I tried to do what you're doing I ended up having to dump something like 300 spaces to be sure the browser would bother with it.

eyelidlessness
A: 

Did you talk with Chrome developers? Did you open a bug about that? IMHO the best solution is make Chrome behave like other browsers do, rather than having a workaround for it.

Okay, actually you probably will need a short-term workaround. But imagine a world in which each browser behaves differently in each aspect, say HTTP, HTML, CSS handling... it would not be a pleasant place!

phjr
Like todays world, where every browser goes on their own way for such corner cases..
artificialidiot
A: 

Stream is working. The answer from eyelidlessness is the solution.

print "2048 points[BR>\n";

The [ = <

BTW look at the user-agent. Safari needs much bytes too. I think 1024. Firefox needs not so much bytes.

A: 

Hi - Is there a reference where to find the requirement for each browser? What I mean is how many bytes is required for each browser to start rendering the response?

Many thanks, jaguarg

A: 
<?php
$i = 0;
while (true) {
  if($i == 0){
    echo "<html><body>";

  }
  echo "<script type='text/javascript'>\n";
  echo "alert('hello');\n";
  echo "</script>";
  if($i == 0 ){
    $padstr = str_pad("",2048,"&nbsp;");
    echo $padstr;
    echo "</body></html>";
  }
    flush();

    sleep(5);
    $i = $i + 1;
}
?>

For fist time send at least 2048 bytes of data. then it will work fine. And make sure to keep script tag in a body tag. The strange thing is , in my case if I add 1024 bytes it worked. Hope this helps you

The above program is working fine in google chrome.

A: 

I had a similar issue to this, and solved it by adding an HTML tag (in my case <br />) before each flush.

My guess would be that Chrome waits for an element which is being displayed to close before triggering a re-render. That's only a guess though.

It didn't seem to require 1024 bytes - I think I would have had just under 512 bytes when it worked.

thomasrutter