0

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

3 Answers 3

1

inside you can put the following lines:

var localScript = document.createElement("script");
localScript.type = "text/javascript";
localScript.src = "localJQ.js";
document.body.appendChild(localScript);
Sign up to request clarification or add additional context in comments.

1 Comment

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!
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.