JS Detect Offline? What Am I doing wrong
===

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>