AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 521


<title>Chapter 10 - Comet Iframe</title>
<script src="http://ajaxref.com/ch10/ajaxtcr.js" type="text/javascript"></script>
<script type="text/javascript">
function sendRequest()
{
var options = {method: "GET",
transport: "iframe",
payload : "output=hellodiv"};
AjaxTCR.comm.sendRequest("http://ajaxref.com/ch10/endlessiframe.php", options);
}
AjaxTCR.util.event.addWindowLoadEvent(sendRequest);
</script>
</head>
<body>
<div id="hellodiv"></div>
</body>
</html>

On the server we generate a response page to go in the iframe transport. We first notice
the code outputs a <script> tag that will call the parent window and put content in the
specified DOM element found in $output, which in our case is “hellodiv.” We also note
that it does this output in an endless loop and flushes the contents out in two-second
intervals.

<?php
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<html>
<head>
<title>No Title Required!</title>
</head>
<body>
<?php
$output = $_GET["output"];
while ($output)
{
print '<script type="text/javascript">';
print 'window.parent.document.getElementById("'. $output. '").innerHTML =
"Hello World at '. date("h:i:s A"). '";';
print '</script>';
ob_flush();
flush();

sleep(2);
}
?>
</body>
</html>
Free download pdf