bomhelper.js
function BomHelper() {
this.ie = "";
this.firefox = "";
this.chrome = "";
this.opera = "";
this.safari = "";
}
//检测浏览器版本,并保存
BomHelper.prototype.checkBrowerType = function () {
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? this.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? this.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? this.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? this.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? this.safari = s[1] : 0;
}
//获取ajax对象
BomHelper.prototype.ajaxObj = function () {
var xmlHttp = null;
if (this.ie != "") {
if (typeof ActiveXObject != "undefined") {
return new XMLHttpRequest();
} else {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (ex1) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (ex2) {
alert("创建ajax对象失败,本网站只支持ie6以上版本浏览器,请刷新页面重试");
}
}
}
} else {
try {
xmlHttp = new XMLHttpRequest();
} catch (ex3) {
alert("创建ajax对象失败,请刷新页面重试");
}
}
return xmlHttp;
}
//发送ajax的GET请求
BomHelper.prototype.ajaxGet = function (sUrl, fnAjax, noSync) {
var xmlHttp = this.ajaxObj();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4)
fnAjax(xmlHttp.responseText);
}
if (sUrl.indexOf("?") == -1)
sUrl = sUrl + "?flesh=" + Math.random();
else
sUrl = sUrl + "&flesh=" + Math.random();
xmlHttp.open("GET", sUrl, (noSync != undefined ? noSync : true));
xmlHttp.send(null);
}
//发送ajax的post请求
BomHelper.prototype.ajaxPost = function (sUrl, sPostData, fnAjax) {
var xmlHttp = this.ajaxObj();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4)
fnAjax(xmlHttp.responseText);
}
if (sPostData == "")
sPostData = sPostData + "flesh=" + Math.random();
else
sPostData = sPostData + "&flesh=" + Math.random();
xmlHttp.open("POST", sUrl, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(sPostData);
}
//同步获取xml文件
BomHelper.prototype.ajaxXml = function (sUrl, sys, fnAjax) {
var xmlHttp = this.ajaxObj();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4)
fnAjax(xmlHttp.responseXML);
}
if (sUrl.indexOf("?") == -1)
sUrl = sUrl + "?flesh=" + Math.random();
else
sUrl = sUrl + "&flesh=" + Math.random();
xmlHttp.open("GET", sUrl, sys);
xmlHttp.send(null);
}
//若是IE7以上版本,则要求它使用IE7
BomHelper.prototype.useIE7 = function () {
document.write("<meta content=\"IE=EmulateIE7\" http-equiv=\"X-UA-Compatible\">");
}
var bomHelper = new BomHelper();
bomHelper.checkBrowerType();
调用方法
bomHelper.ajaxGet("/api/time", function (data) {
var t = new Date(data.substr(0, 4), parseInt(data.substr(4, 2)) - 1, data.substr(6, 2), data.substr(8, 2), data.substr(10, 2), data.substr(10, 2));
timeDiff = parseInt(((new Date()).getTime() - t.getTime()) / 1000);
console.log(data)//20190821104926
console.log(data.substr(0, 4))//2019
console.log(new Date().getTime())//1566355766810
console.log(t.getTime() / 1000)//1566355789
console.log(t)//Wed Aug 21 2019 10:49:49 GMT+0800 (中国标准时间)
console.log(timeDiff)//-22
});