Reputation: 1325
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 some js when file in src doesn't load, but don't know any way to get the file load there.
Thanks in advance
Upvotes: 0
Views: 381
Reputation: 1676
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.
Upvotes: 0
Reputation: 37007
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.
Upvotes: 0
Reputation: 37741
inside you can put the following lines:
var localScript = document.createElement("script");
localScript.type = "text/javascript";
localScript.src = "localJQ.js";
document.body.appendChild(localScript);
Upvotes: 1