基础模块 网络连接检查 js
//无对象则加载
if (typeof Base == "undefined") Base = function() {}
// 获取时间对象的基本方法
Base.prototype.getTime = function() {
var date = new Date();
var yyyy = date.getFullYear(); //四位年份
var month = date.getMonth() + 1; //月份 0-11
var day = date.getDate(); //日
var HH = date.getHours(); //时
var minute = date.getMinutes(); //分钟
var second = date.getSeconds(); //秒
var milliseconds = date.getMilliseconds(); //毫秒
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
if (HH < 10) {
HH = "0" + HH;
}
if (minute < 10) {
minute = "0" + minute;
}
if (second < 10) {
second = "0" + second;
}
var time = yyyy + "-" + month + "-" + day + " " + HH + ":" + minute + ":" + second + " " + milliseconds;
var timeTxt = yyyy + month + day + HH + minute + second;
var times = {
DateTime: time,
TimeTxt: timeTxt
}
return times;
}
// 浏览器检测,获取,弹出框提醒IE 返回浏览器详情
Base.prototype.getBrowserAndSystem = function() {
var BrowserMatch = {
init: function() {
this.browser = this.getBrowser().browser || "An Unknown Browser";
this.version = this.getBrowser().version || "An Unknown Version";
this.OS = this.getOS() || "An Unknown OS";
},
getOS: function() {
if (navigator.platform.indexOf("Win") != -1) return "Windows";
if (navigator.platform.indexOf("Mac") != -1) return "Mac";
if (navigator.platform.indexOf("Linux") != -1) return "Linux";
if (navigator.userAgent.indexOf("iPhone") != -1) return "iPhone/iPod";
},
getBrowser: function() {
var browser = "Unknown Browse";
var version = 0;
var rMsie = /(msie\s|trident\/7)([\w\.]+)/;
var rTrident = /(trident)\/([\w.]+)/;
var rFirefox = /(firefox)\/([\w.]+)/;
var rOpera = /(opera).+version\/([\w.]+)/;
var rNewOpera = /(opr)\/(.+)/;
var rChrome = /(chrome)\/([\w.]+)/;
var rSafari = /version\/([\w.]+).*(safari)/;
var ua = navigator.userAgent.toLowerCase();
var matchBS, matchBS2;
matchBS = rMsie.exec(ua);
if (matchBS != null) {
matchBS2 = rTrident.exec(ua);
if (matchBS2 != null) {
switch (matchBS2[2]) {
case "4.0":
return {
browser: "IE",
version: "8"
};
break;
case "5.0":
return {
browser: "IE",
version: "9"
};
break;
case "6.0":
return {
browser: "IE",
version: "10"
};
break;
case "7.0":
return {
browser: "IE",
version: "11"
};
break;
default:
return {
browser: "IE",
version: "Undefined"
};
}
} else {
return {
browser: "IE",
version: matchBS[2] || "0"
};
}
}
matchBS = rFirefox.exec(ua);
if ((matchBS != null) && (!(window.attachEvent)) && (!(window.chrome)) && (!(window.opera))) {
return {
browser: matchBS[1] || "",
version: matchBS[2] || "0"
};
}
matchBS = rOpera.exec(ua);
if ((matchBS != null) && (!(window.attachEvent))) {
return {
browser: matchBS[1] || "",
version: matchBS[2] || "0"
};
}
matchBS = rChrome.exec(ua);
if ((matchBS != null) && (!!(window.chrome)) && (!(window.attachEvent))) {
matchBS2 = rNewOpera.exec(ua);
if (matchBS2 == null) {
return {
browser: matchBS[1] || "",
version: matchBS[2] || "0"
};
} else {
return {
browser: "Opera",
version: matchBS2[2] || "0"
};
}
}
matchBS = rSafari.exec(ua);
if ((matchBS != null) && (!(window.attachEvent)) && (!(window.chrome)) && (!(window.opera))) {
return {
browser: matchBS[2] || "",
version: matchBS[1] || "0"
};
}
}
};
BrowserMatch.init();
// 获取浏览器名:BrowserMatch.browser;
// 获取浏览器版本:BrowserMatch.version;
// 获取所处操作系统:BrowserMatch.OS;
if (BrowserMatch.browser == "IE" && BrowserMatch.version < 9) {
alert("您当前浏览器为:" +
BrowserMatch.browser +
"\nVersion:" +
BrowserMatch.version + ".0" +
"\n所处操作系统为:" +
BrowserMatch.OS
+
"\n你当前的浏览器不支持本网页大多功能, " +
"\n为了更好体验宝宝提供的服务,\n请点点你的小手手升级或换个更厉害的浏览器吧!"
);
}
return "您当前浏览器为:" + BrowserMatch.browser + "\nVersion:" + BrowserMatch.version + ".0" + "\n所处操作系统为:" + BrowserMatch.OS;
}
//外部网络连接状态检测
Base.prototype.connectionLisner = function() {//onload="conectSuccess()"onerror="conectError()"
$("body").prepend('<img id="connect-test" style="display:none;" /> ');
var imgPath = "https://www.baidu.com/img/bd_logo1.png";
var timeStamp = Date.parse(new Date());
$("#connect-test").attr("src", imgPath + "?timestamp=" + timeStamp);
$("#connect-test").load(function(){alert("Network connect!");});
$("#connect-test").error(function(){alert("Network disconnect!");});
}
// 浏览器动态数据检测未联机执行函数
function conectError() {
//网络不可访问时加载
//
}
// 浏览器动态数据检测 联机执行函数
function conectSuccess() {
//
//网络可访问时加载
//document.write("<scr"+"ipt src=\"##.js\"></sc"+"ript>");
}
$(document).ready(function() {
var b = new Base();
// alert(b.getTime().DateTime);
b.connectionLisner();
});