本文共 1220 字,大约阅读时间需要 4 分钟。
显示服务器时间并一直显示
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head> <title>获取服务器端时间并不停网页在显示上</title> </head><body>
<span id="spnTime"></span> <script language="javascript"> function createXMLHttpRequest () { var request = false; if(window.XMLHttpRequest) { request = new XMLHttpRequest(); if(request.overrideMimeType) { request.overrideMimeType('text/xml'); } } else if(window.ActiveXObject) { var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP']; for(var i=0; i<versions.length; i++) { try { request = new ActiveXObject(versions[i]); if(request) { return request; } } catch(e) { //alert(e.message); } } } return request; } var http = createXMLHttpRequest(); http.open("HEAD", ".", false); http.send(null); var curDate = new Date; var offsetTime = curDate - Date.parse(http.getResponseHeader("Date")); setInterval(function() { curDate.setTime(new Date - offsetTime); document.getElementById("spnTime").innerHTML = curDate.toLocaleString(); }, 1000); </script></body>
</html>