views:

167

answers:

4

I want to get over a nasty problem that shows up yesterday during a demo to a client. We're using jquery, loading it from google api. But yesterday, our ISP begin to cause some problems, and didn't load jq.js properly.

So, what I really want is to load a local file from the server if google api has an extrange behaviour (not that it's going to happen often, but at least doing local demos we won't get harmed again).

I know that <script type="txt/javascript" src="googleapi"> somejs </script> executes somejs when file in src doesn't load, but don't know any way to get the file load there.

Thanks in advance

A: 

Edit : I was a bit jumpy. The solution given by peirix is correct. I'll just use his code, to not let the wrong solution pollute this area. You can do

var localScript = document.createElement("script");
localScript.type = "text/javascript";
localScript.src = "localJQ.js";
document.body.appendChild(localScript);

to load javascript dynamically.

Even this is prone to race conditions, however, if there are scripts in your page that depend on this script. Use with care.

This presentation - Even Faster Websites - elaborates on more techniques to load external files through javascript.

sandesh247
+1  A: 

inside you can put the following lines:

var localScript = document.createElement("script");
localScript.type = "text/javascript";
localScript.src = "localJQ.js";
document.body.appendChild(localScript);
peirix
But as sadesh247 has pointed out if there are others scripts that depend on it, they won't work as expected. I'll mark this as the answer while looking how to fix it the way I need.Thanks!
MaLKaV_eS
A: 

Teaching granny to suck eggs possibly, but why not just use a local copy on the demo machine always?

marcus.greasly
A: 

This isn't directly answering your question, but JQuery's creator John Resig has an interesting blog post about executing the script tag contents when there is also a src attribute.

Stephen Denne