1- ( function ( w ) {
2- w . internetConnection = w . internetConnection || { } ;
1+ function getterSetter ( variableParent , variableName , getterFunction , setterFunction ) {
2+ if ( Object . defineProperty ) {
3+ Object . defineProperty ( variableParent , variableName , {
4+ get : getterFunction ,
5+ set : setterFunction
6+ } ) ;
7+ }
8+ else if ( document . __defineGetter__ ) {
9+ variableParent . __defineGetter__ ( variableName , getterFunction ) ;
10+ variableParent . __defineSetter__ ( variableName , setterFunction ) ;
11+ }
12+ }
313
4- w . internetConnection . addEvent = function ( obj , type , callback ) {
5- if ( window . attachEvent ) {
6- obj . attachEvent ( 'on' + type , callback ) ;
14+ ( function ( w ) {
15+ w . onlinejs = w . onlinejs || { } ;
16+
17+ //Checks interval can be changed in runtime
18+ w . onLineCheckTimeout = 5000 ;
19+
20+ //Use window.onLineURL incapsulated variable
21+ w . onlinejs . _onLineURL = "http://offlinejs.com/online.php" ;
22+
23+ w . onlinejs . setOnLineURL = function ( newURL ) {
24+ w . onlinejs . _onLineURL = newURL ;
25+ w . onlinejs . getStatusFromNavigatorOnLine ( ) ;
26+ }
27+
28+ w . onlinejs . getOnLineURL = function ( ) {
29+ return w . onlinejs . _onLineURL ;
30+ }
31+
32+ getterSetter ( w , 'onLineURL' , w . onlinejs . getOnLineURL , w . onlinejs . setOnLineURL ) ;
33+
34+
35+ //Verification logic
36+ w . onlinejs . setStatus = function ( newStatus ) {
37+ w . onlinejs . fireHandlerDependOnStatus ( newStatus ) ;
38+ w . onLine = newStatus ;
39+ }
40+
41+ w . onlinejs . fireHandlerDependOnStatus = function ( newStatus ) {
42+ if ( newStatus === true && w . onLineHandler !== undefined && ( w . onLine !== true || w . onlinejs . handlerFired === false ) ) {
43+ w . onLineHandler ( ) ;
44+ }
45+ if ( newStatus === false && w . offLineHandler !== undefined && ( w . onLine !== false || w . onlinejs . handlerFired === false ) ) {
46+ w . offLineHandler ( ) ;
47+ }
48+ w . onlinejs . handlerFired = true ;
49+ } ;
50+
51+ w . onlinejs . startCheck = function ( ) {
52+ setInterval ( "window.onlinejs.logic.checkConnectionWithRequest(true)" , w . onLineCheckTimeout ) ;
53+ }
54+
55+ w . onlinejs . stopCheck = function ( ) {
56+ clearInterval ( "window.onlinejs.logic.checkConnectionWithRequest(true)" , w . onLineCheckTimeout ) ;
57+ }
58+
59+ w . checkOnLine = function ( ) {
60+ w . onlinejs . logic . checkConnectionWithRequest ( false ) ;
61+ }
62+
63+ w . onlinejs . getOnLineCheckURL = function ( ) {
64+ return w . onlinejs . _onLineURL + '?r=' + Math . random ( ) ;
65+ }
66+
67+ w . onlinejs . getStatusFromNavigatorOnLine = function ( ) {
68+ if ( w . navigator . onLine !== undefined ) {
69+ w . onlinejs . setStatus ( w . navigator . onLine ) ;
770 } else {
8- obj . addEventListener ( type , callback ) ;
9- }
71+ w . onlinejs . setStatus ( true ) ;
72+ }
1073 }
1174
75+ //Network transport layer
1276 var xmlhttp = new XMLHttpRequest ( ) ;
1377
14- w . internetConnection . isXMLHttp = function ( ) {
78+ w . onlinejs . isXMLHttp = function ( ) {
1579 return "withCredentials" in xmlhttp ;
1680 }
1781
18- w . internetConnection . isXDomain = function ( ) {
19- return typeof XDomainRequest != "undefined" ;
82+ w . onlinejs . isXDomain = function ( ) {
83+ return typeof XDomainRequest != "undefined" ;
2084 }
2185
2286 //For IE we use XDomainRequest and sometimes it uses a bit different logic, so adding decorator for this
23- w . internetConnection . XDomainLogic = {
24- init : function ( ) {
87+ w . onlinejs . XDomainLogic = {
88+ init : function ( ) {
2589 xmlhttp = new XDomainRequest ( ) ;
26- xmlhttp . onerror = function ( ) {
90+ xmlhttp . onerror = function ( ) {
2791 xmlhttp . status = 404 ;
28- w . internetConnection . processXmlhttpStatus ( ) ;
92+ w . onlinejs . processXmlhttpStatus ( ) ;
2993 }
30- xmlhttp . ontimeout = function ( ) {
94+ xmlhttp . ontimeout = function ( ) {
3195 xmlhttp . status = 404 ;
32- w . internetConnection . processXmlhttpStatus ( ) ;
96+ w . onlinejs . processXmlhttpStatus ( ) ;
3397 }
3498 } ,
35- onInternetAsyncStatus : function ( ) {
99+ onInternetAsyncStatus : function ( ) {
36100 try {
37101 xmlhttp . status = 200 ;
38- w . internetConnection . processXmlhttpStatus ( ) ;
39- } catch ( err ) {
40- w . internetConnection . fireHandlerDependOnStatus ( false ) ;
41- w . onLine = false ;
102+ w . onlinejs . processXmlhttpStatus ( ) ;
103+ } catch ( err ) {
104+ w . onlinejs . setStatus ( false ) ;
42105 }
43106 } ,
44- checkConnectionWithRequest : function ( async ) {
45- xmlhttp . onload = w . internetConnection . logic . onInternetAsyncStatus ;
107+ checkConnectionWithRequest : function ( async ) {
108+ xmlhttp . onload = w . onlinejs . logic . onInternetAsyncStatus ;
46109
47- var url = w . onLineCheckURL ( ) ;
110+ var url = w . onlinejs . getOnLineCheckURL ( ) ;
48111
49112 xmlhttp . open ( "GET" , url ) ;
50- xmlhttp . send ( ) ;
113+ w . onlinejs . tryToSend ( xmlhttp ) ;
51114 }
52115 }
53116
54117 //Another case for decoration is XMLHttpRequest
55- w . internetConnection . XMLHttpLogic = {
56- init : function ( ) {
118+ w . onlinejs . XMLHttpLogic = {
119+ init : function ( ) {
57120
58121 } ,
59- onInternetAsyncStatus : function ( ) {
60- if ( xmlhttp . readyState === 4 ) {
122+ onInternetAsyncStatus : function ( ) {
123+ if ( xmlhttp . readyState === 4 ) {
61124 try {
62- w . internetConnection . processXmlhttpStatus ( ) ;
63- } catch ( err ) {
64- w . internetConnection . fireHandlerDependOnStatus ( false ) ;
65- w . onLine = false ;
125+ w . onlinejs . processXmlhttpStatus ( ) ;
126+ } catch ( err ) {
127+ w . onlinejs . setStatus ( false ) ;
66128 }
67129 }
68130 } ,
69- checkConnectionWithRequest : function ( async ) {
131+ checkConnectionWithRequest : function ( async ) {
70132 if ( async ) {
71- xmlhttp . onreadystatechange = w . internetConnection . logic . onInternetAsyncStatus ;
133+ xmlhttp . onreadystatechange = w . onlinejs . logic . onInternetAsyncStatus ;
72134 } else {
73135 xmlhttp . onreadystatechange = undefined ;
74136 }
75137
76- var url = w . onLineCheckURL ( ) ;
77- xmlhttp . open ( "HEAD" , url , async ) ;
78- xmlhttp . send ( ) ;
138+ var url = w . onlinejs . getOnLineCheckURL ( ) ;
139+ xmlhttp . open ( "HEAD" , url , async ) ;
140+ w . onlinejs . tryToSend ( xmlhttp ) ;
79141
80142 if ( async === false ) {
81- w . internetConnection . processXmlhttpStatus ( ) ;
143+ w . onlinejs . processXmlhttpStatus ( ) ;
82144 return w . onLine ;
83- }
145+ }
84146 }
85147 }
86148
87- if ( w . internetConnection . isXDomain ( ) ) {
88- w . internetConnection . logic = w . internetConnection . XDomainLogic ;
149+ if ( w . onlinejs . isXDomain ( ) ) {
150+ w . onlinejs . logic = w . onlinejs . XDomainLogic ;
89151 } else {
90- w . internetConnection . logic = w . internetConnection . XMLHttpLogic ;
152+ w . onlinejs . logic = w . onlinejs . XMLHttpLogic ;
91153 }
92154
93- w . internetConnection . logic . init ( ) ;
94-
95- w . internetConnection . processXmlhttpStatus = function ( ) {
96- var tempOnLine = w . internetConnection . verifyStatus ( xmlhttp . status ) ;
97- w . internetConnection . fireHandlerDependOnStatus ( tempOnLine ) ;
98- w . onLine = tempOnLine ;
155+ w . onlinejs . processXmlhttpStatus = function ( ) {
156+ var tempOnLine = w . onlinejs . verifyStatus ( xmlhttp . status ) ;
157+ w . onlinejs . setStatus ( tempOnLine ) ;
99158 }
100159
101- w . internetConnection . verifyStatus = function ( status ) {
102- return ( status >= 200 && status < 300 || status === 304 )
160+ w . onlinejs . verifyStatus = function ( status ) {
161+ return ( status >= 200 && status < 300 || status === 304 )
103162 }
104163
105- w . internetConnection . fireHandlerDependOnStatus = function ( newStatus ) {
106- if ( newStatus === true && w . onLineHandler !== undefined && ( w . onLine !== true || w . internetConnection . handlerFired === false ) ) {
107- w . onLineHandler ( ) ;
108- }
109- if ( newStatus === false && w . offLineHandler !== undefined && ( w . onLine !== false || w . internetConnection . handlerFired === false ) ) {
110- w . offLineHandler ( ) ;
164+ w . onlinejs . tryToSend = function ( xmlhttprequest ) {
165+ try {
166+ xmlhttprequest . send ( ) ;
167+ } catch ( e ) {
168+ w . onlinejs . setStatus ( false ) ;
111169 }
112- w . internetConnection . handlerFired = true ;
113- }
114-
115- w . internetConnection . startCheck = function ( ) {
116- setInterval ( "window.internetConnection.logic.checkConnectionWithRequest(true)" , w . onLineCheckTimeout ) ;
117- }
118-
119- w . internetConnection . stopCheck = function ( ) {
120- clearInterval ( "window.internetConnection.logic.checkConnectionWithRequest(true)" , w . onLineCheckTimeout ) ;
121170 }
122171
123- w . checkOnLine = function ( ) {
124- w . internetConnection . logic . checkConnectionWithRequest ( false ) ;
125- }
126-
127- w . onLineCheckURL = function ( ) {
128- return "http://offlinejs.com/online.php?r=" + Math . random ( ) ;
172+ //Events handling
173+ w . onlinejs . addEvent = function ( obj , type , callback ) {
174+ if ( window . attachEvent ) {
175+ obj . attachEvent ( 'on' + type , callback ) ;
176+ } else {
177+ obj . addEventListener ( type , callback ) ;
178+ }
129179 }
130180
131- w . onLineCheckTimeout = 5000 ;
132- w . checkOnLine ( ) ;
133- w . internetConnection . startCheck ( ) ;
134- w . internetConnection . handlerFired = false ;
135-
136- w . internetConnection . addEvent ( w , 'load' , function ( ) {
137- w . internetConnection . fireHandlerDependOnStatus ( w . onLine ) ;
181+ w . onlinejs . addEvent ( w , 'load' , function ( ) {
182+ w . onlinejs . fireHandlerDependOnStatus ( w . onLine ) ;
138183 } ) ;
139184
140- w . internetConnection . addEvent ( w , 'online' , function ( ) {
141- window . internetConnection . logic . checkConnectionWithRequest ( true ) ;
142- } ) ;
143- w . internetConnection . addEvent ( w , 'offline' , function ( ) {
144- window . internetConnection . logic . checkConnectionWithRequest ( true ) ;
145- } ) ;
185+ w . onlinejs . addEvent ( w , 'online' , function ( ) {
186+ window . onlinejs . logic . checkConnectionWithRequest ( true ) ;
187+ } )
188+
189+ w . onlinejs . addEvent ( w , 'offline' , function ( ) {
190+ window . onlinejs . logic . checkConnectionWithRequest ( true ) ;
191+ } )
192+
193+ w . onlinejs . getStatusFromNavigatorOnLine ( ) ;
194+ w . onlinejs . logic . init ( ) ;
195+ w . checkOnLine ( ) ;
196+ w . onlinejs . startCheck ( ) ;
197+ w . onlinejs . handlerFired = false ;
146198} ) ( window ) ;
0 commit comments