| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 5 |
|
| 6 |
Use of this source code is governed by the Apache License, Version 2.0. |
| 7 |
See the COPYING file for details. |
| 8 |
--> |
| 9 |
<!-- |
| 10 |
Author: [email protected] (Erik Arvidsson) |
| 11 |
--> |
| 12 |
<head> |
| 13 |
<title>goog.net.OnlineHandler</title> |
| 14 |
<script src="../base.js"></script> |
| 15 |
<script> |
| 16 |
|
| 17 |
goog.require('goog.events'); |
| 18 |
goog.require('goog.events.OnlineHandler'); |
| 19 |
goog.require('goog.net.NetworkStatusMonitor'); |
| 20 |
|
| 21 |
</script> |
| 22 |
<style> |
| 23 |
|
| 24 |
body { |
| 25 |
width: 30em; |
| 26 |
margin: 1em auto; |
| 27 |
text-align: justify; |
| 28 |
font: small sans-serif; |
| 29 |
} |
| 30 |
|
| 31 |
#out.online { |
| 32 |
border: 1px solid green; |
| 33 |
background: lightgreen; |
| 34 |
} |
| 35 |
|
| 36 |
#out.offline { |
| 37 |
border: 1px solid red; |
| 38 |
background: pink; |
| 39 |
} |
| 40 |
|
| 41 |
p { |
| 42 |
padding: 0.2em; |
| 43 |
} |
| 44 |
|
| 45 |
</style> |
| 46 |
</head> |
| 47 |
<body> |
| 48 |
|
| 49 |
<p>This page reports whether your browser is online or offline. It will detect |
| 50 |
changes to the reported state and fire events when this changes. The |
| 51 |
OnlineHandler acts as a wrapper around the HTML5 events <code>online</code> and |
| 52 |
<code>offline</code> and emulates these for older browsers.</p> |
| 53 |
|
| 54 |
<p>Try changing <strong>File -> Work Offline</strong> in your browser.</p> |
| 55 |
|
| 56 |
<p id=out></p> |
| 57 |
|
| 58 |
<script> |
| 59 |
|
| 60 |
var out = document.getElementById('out'); |
| 61 |
var oh = new goog.events.OnlineHandler; |
| 62 |
|
| 63 |
function updateText() { |
| 64 |
out.innerHTML = 'Is online: ' + oh.isOnline(); |
| 65 |
out.className = oh.isOnline() ? 'online' : 'offline'; |
| 66 |
} |
| 67 |
|
| 68 |
goog.events.listen(oh, [goog.net.NetworkStatusMonitor.EventType.ONLINE, |
| 69 |
goog.net.NetworkStatusMonitor.EventType.OFFLINE], |
| 70 |
updateText); |
| 71 |
goog.events.listen(window, 'unload', function() { |
| 72 |
oh.dispose(); |
| 73 |
}); |
| 74 |
|
| 75 |
updateText(); |
| 76 |
|
| 77 |
</script> |
| 78 |
</body> |
| 79 |
</html> |