在页面中加载远程JavaScript文件

在页面中加载远程JavaScript文件

本文介绍了在页面中加载远程JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的页面,其中在页面中加载了多个远程JavaScript文件(我需要它们,并且不能具有本地副本).

I've a simple page in which in the head I load several remote JavaScript files (I need them and can't have a local copy).

我加载的脚本类似于以下内容:

The scripts I load are similar to these:

<script type="text/javascript" src="http://mymachine/donald/script1.js"></script>
<script type="text/javascript" src="http://mymachine/donald/script2.js"></script>

在页面的第一次加载中,我收到了一些关于与这两个脚本相关的对象的错误消息.因此,我认为在脚本下载和执行之前,页面已呈现.如果我尝试重新加载同一页面(缓存中加载了脚本),则不会出现任何错误.

In the first load of the page I got several error messages regarding objects that are related to these two scripts. So I think the page gets rendered BEFORE the scripts are downloaded and executed. If I try to reload the same page (with the cache loaded with the scripts) I don't get any errors.

但是,如果我尝试两次下载每个文件,即使浏览器的缓存是干净的,也不会出现任何错误.我认为这是一种不好的方法,但是我不知道有其他选择.

However if I try to download each file two times, I don't get any error even if the cache of the browser is clean. I think it is a bad approach but I don't know any alternative.

是否存在一个标签,除非下载了脚本,否则该标签可让我停止页面的呈现?还是浏览器的选项? (我不喜欢这种选择,但可能只是其中一种).我只需要支持Internet Explorer 7.

Is there a tag that permits me to stop the rendering of the page unless the scripts are downloaded? Or an option of the browser? (I don't like this alternative but it could be only one). I need to support only Internet Explorer 7.

我已经使用类似于这种方法的js的双重加载部分解决了问题:

I have partially solved using a double loading of the js similar to this one:

但这是正确的吗?

<script type="text/javascript" src="http://mymachine/donald/script1.js"></script>
<script type="text/javascript" src="http://mymachine/donald/script1.js"></script>
<script type="text/javascript" src="http://mymachine/donald/script2.js"></script>
<script type="text/javascript" src="http://mymachine/donald/script2.js"></script>

库不相互依赖.实际上,如果交替加载script1和script2,错误仍然存​​在.

the libraries are not dependant to each other. In fact if load alternatevly script1 and script2 the errors remains.

推荐答案

您应该尝试将这些标签在</body>之前的页面上压得很低.如果只需要支持IE7,则可以解决您遇到的问题.

You should try pushing these tags very low on the page just before the </body>. If you only need to support IE7 this may solve the problem you are having.

这篇关于在页面中加载远程JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 11:39