昨天給一個用戶訪了一個金融站點,在站點頁面的右上角有一個獲取服務器時間的JS特效代碼,361源碼覺得還不錯,就拿出來分享給大家:
<!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獲取服務器時間代碼實例-361源碼-www.49028c.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的方式請求當前文件(361源碼 www.49028c.com) xhr.open("get","/"); xhr.send(null); // 監聽請求狀態變化(361源碼) xhr.onreadystatechange = function(){ var time = null, curDate = null; if(xhr.readyState===2){ // 獲取響應頭里的時間戳(www.49028c.com) time = xhr.getResponseHeader("Date"); console.log(xhr.getAllResponseHeaders()) curDate = new Date(time); document.getElementById("time").innerHTML = "361源碼(www.49028c.com)提示您服務器時間是:"+curDate.getFullYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+" "+curDate.getHours()+":"+curDate.getMinutes()+":"+curDate.getSeconds(); } } }</script></body></html>
直接復制代碼到網站頁面,投放到站點空間測試就能使用。需要注意的一點是:本地測試無效,需要上傳到站點。
新聞熱點
疑難解答