上一篇:加拿大买卖房的几个网站,备份一下,买房时可能用的上
下一篇:很有特色的反馈留言(feedback)
应用Ajax、PHP、javascript来实现异步操作
米娅 2012年1月9日 13:08:50

为了提高网页速度,我计划是使用Ajax、PHP、javascript来实现异步操作,就是每次打开网页后,网页发送一个消息给服务器,服务器自己运行一个程序,这里是runonserver.php,更新内容,等网页刷新时,从服务器获取新的网页内容,这样可以提高网页的速度。

代码如下:



2楼 2012年1月9日 13:12:52 米娅

我发现如果传给服务器的字符串相同,服务器的runonserver.php就不运行,所以就用PHP的函数获得当前的时间,作为字符串发给服务器端。下面的代码是每次页面得到焦点,就会发消息给服务器,服务器的runonserver.php就运行一次。

客户端代码:

<html>
<head>

</head>

<body onFocus="showHint('<?php echo time(); ?>')">
 
<p>The information from the server: <span id="txtHint"></span></p> 


</body>
<script type="text/javascript">
function showHint(str)
{
var xmlhttp;
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }  
  
xmlhttp.open("GET","runonserver.php?q="+str,true);
xmlhttp.send();
}

</script>

</html>


3楼 2012年1月9日 13:15:16 米娅

服务器端代码:

<?php 
  
// 这里省略的,反馈的消息hello会显示在客户端的页面上。  

echo "hello";


?>


4楼 2012年1月9日 13:18:14 米娅

打开客户端网页,显示:

The information from the server: hello

第1页 共1页
相关链接
1. Drupal 7.19: Notice: Undefined index: description in simpletest_test_form()
2. 手机卡成功剪卡装进iphone 5
3. iPhone 5 YouTube视频无法播放(已解决)
4. 整站下载工具WebZip
5. 如何用Adobe Flash CS4和ActionScript 3.0添加超级链接(附代码)
6. 学习Adobe Flash ActionScript的推荐链接
7. 网页右键禁用JavaScript代码
8. 下载Eclipse的链接
9. 加拿大邮票: Canadian Diplomacy (2009)
10. 在eBay上卖东西(7):如何合并邮包优惠邮费(combine the shipping fee)
11. 在eBay上卖东西(3):什么是Print a shipping label
12. Boxing Day的多伦多Nathan Phillips广场(摄影)
13. iphone里的内置指南针定位
14. javascript中的冒号作什么用
15. 用javascript判断字符串是否为正整数
16. 用javascript判断一个对象是否存在