gpt4 book ai didi

Loading remote JavaScript file in a page(在页面中加载远程JavaScript文件)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:51 28 4
gpt4 key购买 nike



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

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



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.

是否有一个标记允许我停止呈现页面,除非下载了脚本?还是浏览器的一个选项?(我不喜欢这种选择,但可能只有一种)。我只需要支持Internet Explorer 7。



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

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



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/script2.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.

这些库并不相互依赖。事实上,如果交替加载script1和script2,错误仍然存在。


更多回答
优秀答案推荐

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

这很奇怪,我认为<head>中的<script>标签确实阻止了页面,直到它们被下载。



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

它们是您部分中的第一个脚本吗?它们至少需要先于任何依赖它们的JavaScript。



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.

您应该尝试将这些标签在之前的页面上推得很低。如果你只需要支持IE7,这可能会解决你的问题。



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

如果您正在执行这些文件中的脚本,请等待文档的加载事件。。。


更多回答

Yes, they are the first scripts in my head section

是的,它们是我头脑中的第一个剧本

Okay. So the other JavaScript on the page, that relies on these files, is within the <body> section?

可以那么页面上依赖于这些文件的其他JavaScript就在部分中?

but there are other javascript files that are using these files. So they have to be the first to be loaded.

但是还有其他javascript文件在使用这些文件。所以他们必须是第一个被装载的。

Then perhaps you should put them all at the bottom in the order of importance.

那么,也许你应该按照重要性的顺序把它们都放在最下面。

I can't touch the rest of the page, only the head section.

我摸不到这一页的其余部分,只有页首部分。

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.

这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留言。

@devundef This sounds like an answer. He is not asking for more information, but giving a possible solution to the problem.

@devunde这听起来像是一个答案。他并不是要求提供更多的信息,而是给出一个可能的解决方案。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com