如何使用javascript加载js文件

时间:2022-01-24 00:12:49

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.

我希望能够克服一个令人讨厌的问题,该问题在昨天向客户演示时出现。我们正在使用jquery,从谷歌api加载它。但昨天,我们的ISP开始出现一些问题,并没有正确加载jq.js。

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).

所以,我真正想要的是从服务器加载一个本地文件,如果谷歌api有一个额外的行为(不是它会经常发生,但至少做本地演示我们不会再受到伤害)。

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

提前致谢

4 个解决方案

#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);

#2


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

教奶奶可能会吸蛋,但为什么不在演示机器上使用本地副本呢?

#3


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

编辑:我有点跳跃。 peirix给出的解决方案是正确的。我只是使用他的代码,不要让错误的解决方案污染这个领域。你可以做

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

to load javascript dynamically.

动态加载JavaScript。

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.

这个演示文稿 - 甚至更快的网站 - 详细阐述了通过javascript加载外部文件的更多技术。

#4


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.

这不是直接回答你的问题,但是当有一个src属性时,JQuery的创建者John Resig有一篇关于执行脚本标记内容的有趣博客文章。

#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);

#2


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

教奶奶可能会吸蛋,但为什么不在演示机器上使用本地副本呢?

#3


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

编辑:我有点跳跃。 peirix给出的解决方案是正确的。我只是使用他的代码,不要让错误的解决方案污染这个领域。你可以做

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

to load javascript dynamically.

动态加载JavaScript。

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.

这个演示文稿 - 甚至更快的网站 - 详细阐述了通过javascript加载外部文件的更多技术。

#4


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.

这不是直接回答你的问题,但是当有一个src属性时,JQuery的创建者John Resig有一篇关于执行脚本标记内容的有趣博客文章。