1

I am working on a web project which will be available offline, simple html, css and js pages. If the user has a working internet connection, an additional js file will be loaded and do stuff on the dom (jQuery, mainly).

There are several ways to test if online connectivity is provided (like https://gist.github.com/jpsilvashy/5725579 and checking the navigator.onLine). I thought, the easiest way is not to test if there is internet and then load a file, but rather just include the (external js) file in the first place. If there is internet, the file will be loaded and execute anyways. My question is: is there any disadvantage of doing this? Like will this be an error on machines that dont have internet?

Alex
  • 9,911
  • 5
  • 33
  • 52

1 Answers1

1

There are some disadvantages, mostly: you have to be careful what you're using on the offline part of the page. Apart from that it's nothing bad, and if used properly, might be easy to integrate. Example using jQuery:

$.getScript( "ajax/test.js" )
    .done(function( script, textStatus ) {
        console.log( textStatus );
    })
    .fail(function( jqxhr, settings, exception ) {
        $( "div.log" ).text( "Triggered ajaxError handler." );
    });
mrówa
  • 5,671
  • 3
  • 27
  • 39
  • can i use the getScript with external files? I'd rather thought about including the js directly in html, belong my other, local files – Alex Oct 04 '13 at 14:23
  • Yes, you can use jQuery.getScript with external files. You can just include this file directly in html, it's not a bad way to do it. Difference is how easy (and readably) is to connect async call with logic that does whatever you want in case of success or failure of getting the file. – mrówa Oct 04 '13 at 14:29