| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
javascript利用xmlhttp获得服务器时钟的方法
作者:佚名    文章来源:不详    点击数:    更新时间:2006-9-14
 

clock.asp
-----------------------------------

< %
Response.Expires = -1
dim a
a=now()
Response.Write formatdatetime(a,2) & " " & formatdatetime(a,3)% >

-----------------------------------
mytime.htm
-----------------------------------

<html>
<body topmargin="0" leftmargin="0"  style="background-color:#e0d0c0">
<table width="98%"><tr>
 <td align="center" width="50%" style="font-size:16;font-weight:bold;">长春轨道客车股份有限公司产品计划价格计算程序</td>
 <td width="50%" align="right">
    <input type="text" style="font-size:12px;border:none;background:;" size="18" id="myTime" />
 </td>
</tr>
</table>
</body>
</html>
<script language="javascript">
//简单方法,用最简单的代码实现,但是有很多错误隐患的
/*
function getClock()
{
 var XmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
 XmlHttp.Open( "POST", "clock.asp", false );
 XmlHttp.Send();
 if (XmlHttp.status == 200) myTime.value=XmlHttp.responseText;
  window.setTimeout("getClock()","1000")
}
setInterval("getClock()",1000);
*/
</script>


如果为了能使程序的兼容性和健壮性更强,可以将mytime.htm改成如下的

-----------------------------------
mytime.htm
-----------------------------------

<html>
<body topmargin="0" leftmargin="0"  style="background-color:#e0d0c0">
<table width="98%"><tr>
 <td align="center" width="50%" style="font-size:16;font-weight:bold;">长春轨道客车股份有限公司产品计划价格计算程序</td>
 <td width="50%" align="right">
    <input type="text" style="font-size:12px;border:none;background:;" size="18" id="myTime" />
 </td>
</tr>
</table>
</body>
</html>

<script>
//复杂方法,添加了很多检测,和错误处理
var xmlhttp,alerted
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
     alert("请安装Microsofts XML parsers")
  }
 }
if (!xmlhttp && !alerted) {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  alert("你的浏览器不支持XMLHttpRequest对象,请升级");
 }
}
function getClock()
{
if (xmlhttp) {
  xmlhttp.Open("Get","clock.asp",true);
  xmlhttp.onreadystatechange=RSchange; 
  xmlhttp.send();
  }
}
setInterval( "getClock()", 1000 );
function RSchange()
{
  if (xmlhttp.readyState==4) {
  myTime.value = xmlhttp.responseText;
  }
 }
</script>


相关文章