Javascript获取服务器时间代码实例

2019-03-08 network gramer 点击: 评论
<!DOCTYPE html PUBLIC "- W3C DTD XHTML 1 0 Transitional EN" "http: www w3 org TR xhtml1 DTD xhtml1-transitional dtd

天晟网小编整理编程教程 > JavaScript > 时看到Javascript获取服务器时间代码实例 ,下面有小编47为你找到的38070相关内容希望对你47有所帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript获取服务器时间代码实例-免费教程 -www.timsion.com</title>
</head>

<body>
<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的方式请求当前文件(免费教程 www.timsion.com)
    xhr.open("get","/");
    xhr.send(null);
    // 监听请求状态变化(免费教程 )
    xhr.onreadystatechange = function(){
      var time = null,
          curDate = null;
      if(xhr.readyState===2){
        // 获取响应头里的时间戳(www.timsion.com)
        time = xhr.getResponseHeader("Date");
        console.log(xhr.getAllResponseHeaders())
        curDate = new Date(time);
        document.getElementById("time").innerHTML = "免费教程 (www.timsion.com)提示您服务器时间是:"+curDate.getFullYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+" "+curDate.getHours()+":"+curDate.getMinutes()+":"+curDate.getSeconds();
      }
    }
  }
</script>
</body>
</html>

文章转载请注明来自天晟网 > 编程教程 > JavaScript >
原文标题:Javascript获取服务器时间代码实例
原文网址:http://www.timsion.com/javas/38070.html

上一篇:javascript中简单的加减法运算教程
下一篇:通过JS实现点击按钮显示或隐藏DIV中的内容

免责声明:以上内容来自互联网和用户投稿,不代表本站的观点和立场,版权归原作者所有,如有侵权,请与我们联系。