现在的位置: 首页 >> 网站编程 >> Javascript >> Javascript利用xmlhttp获得服务器时钟的方法
添加时间:2005-11-16 来源:网教中国 作者:
Javascript利用xmlhttp获得服务器时钟的方法

-----------------------------------
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>


上一篇:setTimeout 和 setInterval 的区别 下一篇:动易首页调用最新文章图片向上滚动方法
大部分文章摘自网上,如有侵犯您的权益请与我们联系,我们会第一时间进行处理,谢谢! [ 打印文章 ] [ 关闭窗口 ]
推荐文章
·JavaScript的脆弱性
·JavaScript 的运算符
·JavaScript 的数据类型
·排序Select中Option项的一个示例
·图片马赛克变化效果 Pic mosaic
·“智慧型”网页制作技巧
·用Javascript自动输出网页文本
·“HTMLMarkup”标记实现网页画中
·菜鸟学习javascript实例教程
·JavaScript实际应用:子窗口和父
相关文章
 
最新文章
·加快 DHTML 的一组技巧
·javascript的键盘控制事件
·DHTML工具栏,Web点击更精彩(8
·DHTML工具栏,Web点击更精彩(7)
·DHTML工具栏,Web点击更精彩(6)
·DHTML工具栏,Web点击更精彩(5)
·DHTML工具栏,Web点击更精彩(4)-
·DHTML工具栏,Web点击更精彩(3)-
·DHTML工具栏,Web点击更精彩(2)-
·DHTML工具栏,Web点击更精彩(1)
Google