开发者

Loading remote JavaScript file in a page

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.

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.

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

but it is correct ?

<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/script开发者_StackOverflow社区2.js"></script>
<script type="text/javascript" src="http://mymachine/donald/script2.js"></script>

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


That’s odd, I thought <script> tags in the <head> did block the page until they got downloaded.

Are they the first scripts in your <head> section? They’ll need to at least come before any JavaScript that relies on them.


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.


if your are executing the scripts from these files - wait for the load event of the document ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜