views:

40

answers:

3
<html>
<head>
<style type="text/css">
    body {
        background-color: #000000;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 20px;
        text-align: center;
        color: #FFFFFF;
    }
</style>
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript">
function sleep(ms)
{
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while (new Date().getTime() < dt.getTime());
}
function update() {
    while (0 < 1) {<?php $contents = file_get_contents("my url here"); 
    ?>
        var count = <?php echo $contents ?>;
        document.getElementById('div').innerHTML = count;
        sleep(1500);
    }
}
</script>
</head>
<body onload=update()>
<iframe id="iframe" src="my url" style="visibility: hidden;"/>
<table width=100% height=100%>
<tr height=80%>
<td width=100%>
<center>
<div id="div"></div>
</center>
</td>
</tr>
</table>
</body>
</html>

When viewing the source code in-browser, the var is set correctly, but it won't set the content of the div. I am positive that the url the PHP script is pointing to is accurate and contains only a script to grep a number from an external page.

Thanks in advance!

+1  A: 

Assuming that the value of file_get_contents("my url here") yields 42 (for example), you are aware that you're creating an infinite loop which will set the content of #div to 42 over and over again every 1.5 seconds? I.e., var count will always, constantly be 42 and won't change, since PHP is evaluated only once on the server.

Furthermore I would suspect that your sleep function could pose a bit of a problem, since it creates another tight loop. I wouldn't be surprised if this script simply blocked the browser without ever updating anything since it's caught in an infinite loop. The way to delay execution in Javascript is to use window.setTimeout or window.setInterval.

I guess what you're looking for is an AJAX solution of some kind, if you would tell us what it is you want to do. Since you're including jQuery (without actually using it), you should have a look at jQuery.get(), coupled with a setInterval to update the div periodically.

Something like:

window.setInterval(function () {
    $.get("my url here", function (data) {
        $('#div').html(data);
    });
}, 1500);
deceze
Yeah, I meant to use jQuery but never did... I'll try this. Thanks!
esqew
THANK YOU!! It worked. :)
esqew
A: 
var count = <?php echo $contents ?>;
document.getElementById('div').innerHTML = count;

You are asking the webserver for variable $contents through php, which will always be a static number unless you reload the page. Try using jQuery, as the poster above mentioned.

Charles Ray
+2  A: 

To answer the question, why isn't it running... your onload needs quotes:

<body onload="update()">

but the others have posted better ways to go about it using jquery.

DGM