0

I have a question about Javascript. I create a web page (HTML+Javascript) and want to run script#1 if the user is online, or script#2 else. I have a small and bad idea:

if(navigator.online) { /*here I want: <script src="script#1.js"></script>*/}else {/*here I want: <script src="script#2.js"></script>*/}

Have you got a better idea?

David
  • 1
  • 2
  • that looks like a duplicate. http://stackoverflow.com/questions/189430/detect-that-the-internet-connection-is-offline – js.palus May 03 '17 at 09:30

1 Answers1

0

The HTML5 Application Cache API specifies

navigator.onLine

it should work on major browsers.

js.palus
  • 83
  • 1
  • 10
  • Hi, maybe you don't understand: if you are online do script#1, else do script#2 (with if-then-else instruction). But how I create this idea? – David May 03 '17 at 09:46
  • 1
    ok, got you now. you mean INCLUDE that script. But it seems a bit overkill, why don't you just have your both file loaded and execute the one you want depending on being online or not ? – js.palus May 03 '17 at 14:10