2

I am a beginner in web developing, and I am trying to achieve this:

I have a script, and I want it to be loaded online, from my host. But, if there's no internet connection (the script fails to load, even in another way), then fallback to load a local script. Is this possible?

I don't mind the solution, if it's jquery or anything else, I'd just like it to work.

Thanks.

Amar Syla
  • 3,523
  • 3
  • 29
  • 69
  • How would you know where to load it from locally? – jfriend00 Mar 26 '13 at 22:12
  • You want to have the browser try to load a script from a CDN and fall back to your host, or...? When you say "locally" do you mean from the filesystem? – Chad Mar 26 '13 at 22:13
  • Have you taken a look at HTML5 `LocalStorage`, if yes, what have you tried? – Roko C. Buljan Mar 26 '13 at 22:13
  • Look, I want to create a web app. This is the script I would like to be loaded first: http://beta.poezishqip.com/recog.json then, if there is no internet connection, load a script from a folder in the web app, ex: src/lib/recog.json I hope I am clear enough. – Amar Syla Mar 26 '13 at 22:13
  • 1
    So you want the javascript to actually load the file form the user's local filesystem? We need to know a lot more about the environment for something like that. Are you using desktop webkit frames, or node.js, or actually in a browser using `file://` protocol, or hitting a remote page or what? – Chad Mar 26 '13 at 22:15
  • Closely related: http://stackoverflow.com/questions/5257923/how-to-load-local-script-files-as-fallback-in-cases-where-cdn-are-blocked-unavai – Benjamin Gruenbaum Mar 26 '13 at 22:15
  • So, your web app is offline/local too? – Felix Kling Mar 26 '13 at 22:15
  • @Chad No, I think you are all misunderstanding. The file won't be loaded from the user's local file system, but the file will be in the web app's directory. BenjaminGruenbaum I tried that, but it didn't work. FelixKling Yes, my app is an HTML5 app which I am planning to upload to appstore as a native app. – Amar Syla Mar 26 '13 at 22:19
  • If it's in the directory of a local web app, how is it not on the users computer ? – adeneo Mar 26 '13 at 22:23
  • Wait a minute, why do you have a javascript file with a .json file extension ? – adeneo Mar 26 '13 at 22:24
  • Damn adeneo, you're not understanding :@ I know that when it's a local web app in a computer, the files will be in a computer too, I am not dumb to don't know that. But, I meant that if my web app loads the file from the user's file system, an example would be with HTML5 local storage, that it stores the file locally, and then the web app loads that script directly from the file system, and not from online or from the web app's directory. – Amar Syla Mar 26 '13 at 22:24
  • Adeneo, I never said that I want to load a javascript, and even if I said, if you don't know, JSON is human readable javascript. – Amar Syla Mar 26 '13 at 22:28
  • 2
    And here I was, thinking it was just a dataformat, and had nothing to do with javascript ? – adeneo Mar 26 '13 at 22:29

3 Answers3

3
<script>
var jsLoaded = false;
setTimeout("callback()", 2000);
function callback(){
  if (!jsLoaded) {
    //Do ur stuff means call your local javascript....
  } 
}
</script>

<script type="text/javascript" src="//assets.zendesk.com/external/zenbox/v2.5/zenbox.js" id="popup" onload="jsLoaded=true"></script>

Explanation:
We have a callback that is called after two seconds and checks for a variable that is set by the onload-event of your script-reference. You can execute code in the callback that loads your local script.

void
  • 881
  • 3
  • 20
  • 27
0

This assumes you have jQuery accessible locally.

Well, I would suggest you "attempt" to load the script from the remote host via jQuery/Ajax, I think they have some tools to help you do this. Then check the response code... I think 200 is "OK", if it's not "OK", then proceed to load the local version.

<script>
$('body').getScript('<yourremotehostaddress>').fail(function(jqxhr, settings, exception) {

       // Load your local script here.

});
</script>

Hope this helps :)

Also a solution from the comments above: You can try to load the script from the remote host and after the attempt try to access an object included in the script.. Assuming there is one.

Update: Sorry for the initial post, I didn't realize that the handler passed to getScript() only gets called if the request succeeds. We want to check for failure.

Andrew Senner
  • 2,479
  • 1
  • 18
  • 24
0

Something like this maybe...?

<script type="text/javascript" src="//cod.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
    if(!window.jQuery) {
        document.write("<script type=\"text/javascript\" src=\"path/to/local/jquery.js\"><\/script>");
    }
</script>
faino
  • 3,194
  • 15
  • 17
  • I tried this one, but it didn't work. First, after I added the code, I loaded the page with that and the script was loaded okay. But, when I closed my internet connection, and tried to load again, if the fallback script loaded, it didn't work. I cleared my cache before reloading it :D – Amar Syla Mar 26 '13 at 22:20
  • I don't understand your answer, "*if jQuery is not bound to window, load another file*" ... how to load it if no connection to internet? :) – Roko C. Buljan Mar 26 '13 at 22:22