Skip to content

Commit bf780fc

Browse files
Chrome bugs fixed
1 parent 046323f commit bf780fc

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ ONLINE JS
33

44
Lightweight and reliable library to check internet connection status.
55

6+
Compatible browsers: IE 8-9, Chrome, Firefox, Android, iOS
7+
8+
69
Usage
710
-----
811

9-
Include this library into your project and just check is <code>window.onLine</code> true.
12+
Include this library into your project and just check is <code>window.onLine === true</code>.
1013

1114
Assign <code>window.onLineHandler</code> or <code>window.offLineHandler</code> functions to handle status changes.
1215

1316
Just look at <a href="http://pixelscommander.com/polygon/onlinejs/">example</a> (index.html in the repo)!
1417

18+
More info in this <a href="http://pixelscommander.com/en/javascript/onlinejs-javascript-internet-connection/">blog post</a>.
19+
20+
21+
navigator.onLine
22+
----------------
23+
24+
I`m often asked: “Why not use just navigator.onLine ?”.
25+
You have to know that this property is underhandled and inconsistent between different browsers. For many of them it shows status of local network connection only and for some versions of FireFox it depends only on autonomic mode settings. So using this property in critical tasks is not good idea. Online JS is a right way for serious project, it uses navigator.onLine only as one of possible triggers and then makes more tests of internet connection.
26+
1527

1628
Questions and propositions
1729
--------------------------

min/online.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/online.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,31 @@
3232
}
3333

3434
w.internetConnection.onInternetAsyncStatus = function (){
35-
try {
36-
if (w.internetConnection.isXDomain()){
37-
xmlhttp.status = 200;
35+
if (xmlhttp.readyState === 4 || w.internetConnection.isXDomain()){
36+
try {
37+
if (w.internetConnection.isXDomain()){
38+
xmlhttp.status = 200;
39+
}
40+
w.internetConnection.processXmlhttpStatus();
41+
} catch(err){
42+
w.internetConnection.fireHandlerDependOnStatus(false);
43+
w.onLine = false;
3844
}
39-
w.internetConnection.processXmlhttpStatus();
40-
} catch(err){
41-
w.internetConnection.fireHandlerDependOnStatus(false);
42-
w.onLine = false;
4345
}
4446
}
4547

4648
w.internetConnection.checkConnectionWithRequest = function (async){
4749
if (xmlhttp!=null){
4850

4951
if (async) {
50-
xmlhttp.onload = w.internetConnection.onInternetAsyncStatus;
52+
if (w.internetConnection.isXDomain()) {
53+
xmlhttp.onload = w.internetConnection.onInternetAsyncStatus;
54+
} else if (w.internetConnection.isXMLHttp()) {
55+
xmlhttp.onreadystatechange = w.internetConnection.onInternetAsyncStatus;
56+
}
5157
} else {
5258
xmlhttp.onload = undefined;
59+
xmlhttp.onreadystatechange = undefined;
5360
}
5461

5562
var url = w.onLineCheckURL();
@@ -117,9 +124,11 @@
117124
});
118125

119126
w.internetConnection.addEvent(w, 'online', function(){
127+
console.log('online');
120128
window.internetConnection.checkConnectionWithRequest(true);
121129
});
122130
w.internetConnection.addEvent(w, 'offline', function(){
131+
console.log('offline');
123132
window.internetConnection.checkConnectionWithRequest(true);
124133
});
125134
})(window);

0 commit comments

Comments
 (0)