1

I am creating an application heavily based on javascript that needs that uses at least two APIs. In the main index file I am loading some external javascript files. I want to create random data when the app is offline (as opposed to real data coming from the API) Is there a way to dynamically load a different file locally if there is not an internet connection?

This is one of the scripts that is being loaded.

Btw, the app is going to run on the latest chrome browser version, so there is no need to cater for fallbacks; HTML5 is fine.

<script src="http://www.google.com/uds/api/search/1.0/9d8da27b023599b9c9de8a149abbd171/default+en.I.js" type="text/javascript"></script>

Mg Gm
  • 151
  • 1
  • 11

1 Answers1

0

You could check if any element or variable from the library exists, and if not, load your own file. I've seen it done with jQuery, but there will be a delay before your browser figures out the remote file is not available, I think, so page loads might be slow while offline.

if(!someVariableThatShouldExist){
  document.write('<script src="mylocalfile.js" type="text/javascript"></script>');
}
Joachim VR
  • 2,320
  • 1
  • 15
  • 24
  • Performance is not an issue in this case, I think this is a valid option, i will try it! – Mg Gm Jul 08 '14 at 13:59
  • Careful this will result in error, look at this new thread! http://stackoverflow.com/questions/24641863/adding-a-script-tag-dynamically-with-document-write/24642007?noredirect=1#comment38194956_24642007 – Mg Gm Jul 08 '14 at 21:59