js获取服务器时间

来源:互联网 发布:淘宝双查号是什么意思 编辑:程序博客网 时间:2024/05/20 15:41
<p id="time"></p><script>ajax()  function ajax(option){    var xhr = null;    if(window.XMLHttpRequest){      xhr = new window.XMLHttpRequest();    }else{ // ie      xhr = new ActiveObject("Microsoft")    }    // 通过get的方式请求当前文件    xhr.open("get","/");    xhr.send(null);    // 监听请求状态变化    xhr.onreadystatechange = function(){      var time = null,          curDate = null;      if(xhr.readyState===2){        // 获取响应头里的时间戳        time = xhr.getResponseHeader("Date");        console.log(xhr.getAllResponseHeaders())        curDate = new Date(time);        document.getElementById("time").innerHTML = "服务器时间是:"+curDate.getFullYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+" "+curDate.getHours()+":"+curDate.getMinutes()+":"+curDate.getSeconds();      }    }  }

</script>

原文地址:http://www.cnblogs.com/wangmeijian/p/4442830.html

0 0
原创粉丝点击