Reputation: 101
I'm a JS newb so I'm trying to figure out how to make this example from w3c to work in my WebApp that will run in a PhoneGap framework...
I'm sure this is easy, but when the event listener is triggered, it runs the attached function with the alert, problem is is it proceeds to excute everything after that again. So say I flip my iphone off, I'll get an alert then it will execute the next alert which says its online... Anywho... If any of you gys have any better ideas about executing a function like this let me know PhoneGap Cordova has two other methods as well.
<script>
<!--
window.addEventListener("offline", function(e) {alert("offline");})
window.addEventListener("online", function(e) {alert("online");})
if (navigator.onLine) {
alert('online')
//functions to run online
} else {
alert('offline');
//offline functions or through into loop
}
-->
</script>
Upvotes: 2
Views: 2500
Reputation: 603
You are missing a parameter in the addEventListener function. For me this is working fine:
window.addEventListener("offline", function () {
console.log("Online status: " + navigator.onLine);
}, false);
window.addEventListener("online", function () {
console.log("Online status: " + navigator.onLine);
}, false);
Upvotes: 1
Reputation: 71
try out this if(window.navigator.onLine) { alert('I am online');}
I have just tested it in console and it works :)
Upvotes: 0