How to load js file using javascript
I want to solve a nasty problem that appears yesterday during a demo to a client. We are using jquery by loading it from google api. But yesterday our ISP started causing some problems and was not loading jq.js properly.
So, I really want to download a local file from the server if the google api has an extrange behavior (not that it happens often, but at least doing local demos won't hurt us anymore).
I know what <script type="txt/javascript" src="googleapi"> somejs </script>
is doing somejs when the file in src doesn't load, but doesn't know how the file can be loaded.
Thanks in advance
a source to share
Edit: I was a little nervous. The solution given by peirix is correct. I just use his code to prevent the wrong solution from polluting 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.
However, it depends on race conditions, however, if your page has scripts that depend on this script. Use with care.
This presentation - Even Faster Websites - details on how to load external files via javascript.
a source to share
This doesn't directly answer your question, but jQuery creator John Resig has an interesting post about executing a script tag when there is also a src attribute.
a source to share