Skip to content

Commit b256dca

Browse files
Requesting only headers to speed up process
1 parent 3fd18f2 commit b256dca

1 file changed

Lines changed: 44 additions & 50 deletions

File tree

src/online.js

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
11
(
22
function (w){
3-
4-
var OnLine = function(w){
53

6-
if (w.XMLHttpRequest){
7-
var xmlhttp=new XMLHttpRequest()
8-
} else if (w.ActiveXObject){
9-
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
10-
}
11-
12-
this.onInternetStatus = function (){
13-
if (xmlhttp.readyState==4){
14-
try {
15-
if (xmlhttp.status==200){
16-
if (w.onlineHandler !== undefined && w.online !== true){
17-
w.onlineHandler();
18-
}
19-
w.online = true;
20-
return;
21-
} else {
22-
if (w.offlineHandler !== undefined && w.online !== false){
23-
w.offlineHandler();
24-
}
25-
w.online = false;
26-
}
27-
} catch(err){
28-
if (w.offlineHandler !== undefined && w.online !== false){
29-
w.offlineHandler();
4+
w.internetConnection = w.internetConnection || {};
5+
6+
var xmlhttp = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );
7+
8+
w.internetConnection.onInternetStatus = function (){
9+
if (xmlhttp.readyState==4){
10+
try {
11+
if (xmlhttp.status==200){
12+
if (w.onLineHandler !== undefined && w.onLine !== true){
13+
w.onLineHandler();
3014
}
31-
w.online = false;
32-
}
33-
}
34-
}
35-
36-
this.checkOnLine = function (){
37-
if (xmlhttp!=null){
38-
xmlhttp.onreadystatechange=this.onInternetStatus;
39-
xmlhttp.open("GET",w.onlineCheckURL,true);
40-
xmlhttp.send(null);
15+
w.onLine = true;
16+
return;
17+
} else {
18+
if (w.offLineHandler !== undefined && w.onLine !== false){
19+
w.offLineHandler();
20+
}
21+
w.onLine = false;
22+
}
23+
} catch(err){
24+
if (w.offLineHandler !== undefined && w.onLine !== false){
25+
w.offLineHandler();
26+
}
27+
w.onLine = false;
4128
}
29+
}
30+
}
31+
32+
w.internetConnection.checkonLine = function (){
33+
if (xmlhttp!=null){
34+
xmlhttp.onreadystatechange=this.onInternetStatus;
35+
xmlhttp.open("HEAD",w.onLineCheckURL,true);
36+
xmlhttp.send();
4237
}
43-
44-
this.startCheck = function (){
45-
window.internetConnection.checkOnLine();
46-
setInterval("window.internetConnection.checkOnLine()",w.onlineCheckTimeout);
47-
}
48-
49-
this.stopCheck = function (){
50-
clearInterval("window.internetConnection.checkOnLine()",w.onlineCheckTimeout);
51-
}
52-
return this;
5338
}
5439

55-
w.onlineCheckURL = "http://www.pixelsresearch.com/online.php";
56-
w.onlineCheckTimeout = 5000;
57-
w.internetConnection = new OnLine(window);
40+
w.internetConnection.startCheck = function (){
41+
w.internetConnection.checkonLine();
42+
setInterval("window.internetConnection.checkonLine()",w.onLineCheckTimeout);
43+
}
44+
45+
w.internetConnection.stopCheck = function (){
46+
clearInterval("window.internetConnection.checkonLine()",w.onLineCheckTimeout);
47+
}
48+
return this;
49+
50+
w.onLineCheckURL = "http://www.pixelsresearch.com/onLine.php?r=" + Math.random();
51+
w.onLineCheckTimeout = 5000;
5852
w.internetConnection.startCheck();
5953
}
60-
)(window);
54+
)(window);

0 commit comments

Comments
 (0)