Javascript利用xmlhttp获得服务器时钟的方法
2005-11-09 09:22
411 查看
-----------------------------------
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>
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>
相关文章推荐
- JavaScript - XMLHttpRequest 属性和方法
- nginx优化-利用nginx限制HTTP的请求方法--防止脚本被上传至服务器运行该脚本对系统的破坏
- javascript XMLHttpRequest.send() 方法
- 解如何利用 XML 和 JavaScript Object Notation 在 Ajax 客户端和 Java 服务器之间传输数据。
- 利用ASP从远程服务器上接收XML数据的方法
- 利用JavaScript+XmlHttp实现DropDownList无刷新联动
- JavaScript--XMLHttpRequest获取数据方法
- javascript XMLHttpRequest 对象的open() 方法参数说明
- 利用ASP从远程服务器上接收XML数据的方法
- 利用XMLHttpRequest(XHR)对象实现与web服务器通信
- 解如何利用 XML 和 JavaScript Object Notation 在 Ajax 客户端和 Java 服务器之间传输数据(代码)(Oracle)。
- 利用HttpClient获得cookie,并在webview中使用的方法
- 一个简单的使用XMLHttpRequest获得服务器数据的例子
- .net使用Javascript XMLHttpRequest在前台调用后台方法
- 利用FORM表单提交到WEBSERVICE后获得XML文档但不跳转的方法
- Ajax学习(一),获得XMLHttpRequest对象,进行初步异步交互(jquery.javascript)
- javascript获取xml对象的方法(使用XMLHttpRequest)
- [原创]利用php和javascript实现服务器时间电子时钟
- 创建XMLHttpRequest对象并向服务器发送请求的方法
- Ajax获取XmlHttpRequest对象的方法,兼容IE、火狐。用来与服务器进行通信。