-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathonline.js
140 lines (134 loc) · 4.61 KB
/
online.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;(function(w){
var xmlhttp = new XMLHttpRequest(),
time=2000,//设置轮询时间
url ="",//当 `url`为空的时候 默认所有浏览器使用 onLine 和 offline事件
OL = OL || {};
OL={
OL_api:{//扩展API
onLineHandler:function(func){
onlinenetwork.online=func
return this
},
offLineHandler:function(func){
onlinenetwork.offline=func
return this
}
},
setStatus:function (newStatus) {
this.eventStatus(newStatus);
w.onLine = newStatus;
},
//状态改变执行事件
eventStatus: function (newStatus) {
if (newStatus === true && onlinenetwork.online !== undefined && (w.onLine !== true || this.handlerFired === false)) {
onlinenetwork.online();
}
if (newStatus === false && onlinenetwork.offline !== undefined && (w.onLine !== false || this.handlerFired === false)) {
onlinenetwork.offline();
}
this.handlerFired = true;
},
//http请求
XMLHttpLogic:function (async) {
var url = this.getOnLineCheckURL(),
self = this;
if(async){
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4) {
try {
self.processXmlhttpStatus();
} catch (err) {
self.setStatus(false);
}
}
};
}else{
xmlhttp.onreadystatechange = undefined;
}
xmlhttp.open("HEAD", url, async)
this.tryToSend(xmlhttp);
},
processXmlhttpStatus: function () {
var tempOnLine = this.verifyStatus(xmlhttp.status);
this.setStatus(tempOnLine);
},
//尝试发送请求
tryToSend:function (xmlhttprequest) {
try {
xmlhttprequest.send();
} catch(e) {
this.setStatus(false);
}
},
//确认状态
verifyStatus:function (status) {
return status === 200;
},
//url加上随机数
getOnLineCheckURL: function () {
return url + '?' + Math.floor(Math.random() * 1000000);
},
//非 chrome 和 Safari 浏览器不停的检查,嘿嘿
startCheck:function(){
var self = this
setInterval(function(){
self.XMLHttpLogic(true)
},time);
},
//第一次检查是否在线
checkOnLine:function(){
this.XMLHttpLogic(false)
},
getStatusFromNavigatorOnLine:function () {
if (w.navigator.onLine !== undefined) {
this.setStatus(w.navigator.onLine);
} else {
this.setStatus(true);
}
},
//判断浏览器
getExplorer: function(newStatus){
var explorer = window.navigator.userAgent;
this.setStatus(newStatus)
console.log(explorer)
console.log(url)
if((explorer.indexOf('Firefox') >= 0 || explorer.indexOf('MSIE') >= 0)&&url){
console.log("getExplorer:1")
this.checkOnLine()
this.setStatus(newStatus)
this.startCheck(newStatus)
}else{
console.log("getExplorer:2")
this.eventStatus(newStatus)
}
},
//绑定事件
addEvent: function (obj, type, callback) {
if (window.attachEvent) obj.attachEvent('on' + type, callback);
else obj.addEventListener(type, callback);
},
init:function(){
var self = this
//获取当前状态
this.addEvent(w, 'load', function () {
self.eventStatus(w.onLine);
});
//侦听 online 事件
this.addEvent(w, 'online', function () { self.getExplorer(true) });
//侦听 offline 事件
this.addEvent(w, 'offline', function () { self.getExplorer(false) });
self.getExplorer(true)
this.handlerFired = false;
}
}
onlinenetwork=function (json){
if(json){
if (json.time) time=json.time;
if (json.url) url=json.url;
}
OL.init()
for (var a in OL.OL_api) this[a]=OL.OL_api[a];
return this
}
w.onlinenetwork=onlinenetwork
})(window);