当前位置: 代码迷 >> Web前端 >> http长连接技术(六)iframe demo的简化
  详细解决方案

http长连接技术(六)iframe demo的简化

热度:120   发布时间:2012-10-30 16:13:36.0
http长连接技术(6)iframe demo的简化
标签:web javascript comet php
通过对这个例子的分析,我们也可以完全不用comet,直接使用iframe.
也可以不用prototype.js
Index.html
显示代码
打印
01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
02 <html xmlns="http://www.w3.org/1999/xhtml">
03   <head>
04     <title>Comet demo</title>
05     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
06      
07   </head>
08   <body>
09     <div id="content">The server time will be shown here</div>
10 <IFRAME id=workerWin name=workerWin src="./backend.php" width=0 height=0></IFRAME>
11 <script type="text/javascript">
12       
13     function printServerTime(time) {
14     //document.getElementById("content").innerHTML = time; //和下面方法等效
15     document.all("content").innerHTML = time;
16     //$(''content'').innerHTML = time; //需要prototype.js
17     
18   }
19
20 </script>
21 </body>
22 </html>






说明:
这里去掉了comet对象,也没有动态生成iframe,而是直接使用显式的iframe定义。
prototype.js在这个例子作用不大,也可以去掉。直接通过dhtml/dom定位页面元素
backend.php
显示代码
打印
01 <?php
02 while(1) {
03   echo ''<script type="text/javascript">'';
04   echo ''window.parent.printServerTime(''.time().'');'';
05   //echo ''alert(1);'';
06   echo ''</script>'';
07   ob_flush();
08   flush(); // used to send the echoed data to the client
09   sleep(1); // a little break to unload the server CPU
10 }
11 ?>





说明:调用的printServerTime函数直接是页面定义的,没有通过comet封装。
  相关解决方案