同步< script>陈述? [英] Synchronous <script> statements?

查看:58
本文介绍了同步< script>陈述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xmlhttprequest (和 eval )动态加载脚本。接下来,我评估脚本并查看是否还有其他脚本要加载。如果脚本中的任何一个导致异常,则报告的错误消息将指示与 eval 关联的行号,而不是与脚本中的实际错误有关。

I am using xmlhttprequest (and eval) to dynamically load scripts. Next I evaluate the script and look to see if there are any other scripts to load. Along the way if any of the scripts cause and exception, the error message reported indicates the line number associated with eval, not with the actual error in the script.

另一个问题,建议我改用< script> 以获得更好的错误消息。不幸的是,< script> 是异步的,我将无法控制脚本的加载顺序(我需要 onload 回调)。

On another question it was suggested I use <script> instead to get better error messages. Unfortunately, <script> is asynchronous, and I won't be able to control the order of loading of scripts (I need an onload callback).

如何在< script> 命令中实现同步行为

How do I implement synchronous behaviour in <script> commands

有关我要实现的目标的更多信息

Some more info as to what I aim to achieve

每个脚本具有加载的其他脚本的列表,并存储在列表中,我们称其为 _toLoad

Every script has a list of other scripts it loads, stored in a list, lets call it _toLoad

让我们说拥有一个脚本 Main.js,其加载列表如下:

Lets say we have a script 'Main.js' with a load list like so

_toLoad = [['A.js'],['B. js'],['C.js'],['D.js','E.js','F.js']]

其中指出,一旦加载,文件 A.js必须接下来加载。加载 A.js后,接下来必须加载 B.js。加载 B.js后,接下来必须加载 C.js。一旦加载了 C.js,就必须以任何顺序加载 D.js, E.js和 F.js。

Which states that once loaded, the file 'A.js' must be loaded next. Once 'A.js' is loaded, 'B.js' must be loaded next. Once 'B.js' is loaded, 'C.js' must be loaded next. Once 'C.js' is loaded, 'D.js' ,'E.js' , and 'F.js' must be loaded, in any order.

我可以使用< script> 加载'Main.js',评估它的 _toLoad 列表并开始加载其他脚本,顺序正确。但是,如果 A.js也加载了多个脚本会怎样?我希望它们在后台加载,而不要延迟'B.js'加载

I could use <script> to load 'Main.js', evaluate it's _toLoad list and start loading the other scripts, in the correct order. But what happens if 'A.js' has several scripts it also loads? I want those to load in the background, and not to delay 'B.js' from loading

如果 A.js具有如下加载列表,该怎么办:

What if 'A.js' has a load list like so:

_toLoad = [['A2.js'],['B2.js'],['C2.js'],['D2 .js, E2.js, F2.js]]

我是否必须经过发布code>< script> 语句。当我想要广度优先时,这似乎是一种深度优先的方法。

Is I would have to go through and issue <script> statements for those. It seems like a depth first approach, when I want a form of breadth first.

推荐答案

其中一个链接问题提到设置脚本标签元素的innerHTML。我认为这可能达到目的

One of the linked questions mentioned setting the innerHTML of the script tag element. I think this might do the trick

function load(toEval, callback){
    __global_callback = callback;

    var node = document.createElement('script');
    node.innerHTML = toEval + '; __global_callback()';
    document.getElementsByTagName('head')[0].appendChild(node);
}

load("console.log('before...');", function(){
    console.log('...and after');
});

这篇关于同步&lt; script&gt;陈述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆