其中的Javascript代码< BODY>在*< SCRIPT>之前运行*< HEAD>满载了吗? [英] Javascript code inside <BODY> runs *before* <SCRIPT>s in <HEAD> are fully loaded?

查看:107
本文介绍了其中的Javascript代码< BODY>在*< SCRIPT>之前运行*< HEAD>满载了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用缩小版本( ext-all.js )时,一切都很好但是当我用 ext-all-debug替换它时。 js (其他一切都完好无损),firebug报告由于我知道的未定义值导致的各种错误,我在Ext之后加载的Ext或其他js文件中定义。 / p>

经过一些试验和错误,我在几个js文件的末尾放了 console.log() s,没有出现。我认为使用 ext-all-debug.js 会以某种方式导致< BODY>< / BODY> 之前执行 c>< HEAD> 中的脚本已完全加载,这会导致未定义错误,因为< BODY> 引用应该预先加载的值。



对吗?

 < HEAD> 
< SCRIPT type =text / javascriptsrc =ext / ext-base.js>< / SCRIPT>
< SCRIPT type =text / javascriptsrc =ext / ext-all-debug.js>< / SCRIPT>
< SCRIPT type =text / javascriptsrc =my_lib.js>< / SCRIPT> <! - 定义myValue - >
< / HEAD>
< BODY>
< SCRIPT type =text / javascript>
console.log(myValue)// undefined
< / SCRIPT>
< / BODY>

这里是 my_lib.js

  var myValue = 10; 
console.log(my_lib.js loaded);


my_lib.js已加载


未显示在控制台中。

解决方案

只要脚本未标记为延迟或异步或动态加载,它们将按照HTML文件中遇到的顺序执行。这是规范的一部分,所以它是有保证的。



如果您认为在早期脚本中定义了 myValue 未定义,那么那里是一些其他问题。



一些可能性是:


  1. 你的一个脚本有一个错误阻止完全执行。

  2. 您的变量未在全局范围内定义,而是在某个子范围内定义,因此当您尝试时,它在全局范围内不可用使用它。

  3. 您的变量由前一个脚本定义,但未成功初始化为 undefined 以外的值。

  4. 浏览器找不到您的脚本(因此没有加载/执行)。

如果您可以向我们展示显示问题的工作/非工作页面,我们很可能会很快发现问题。由于现在没有显示问题的样本页面,我们只能推测可能的原因。


When I use the minified version (ext-all.js) everything is fine but when I replace it with ext-all-debug.js (everything else intact), firebug reports all sorts of errors due to undefined values that I know are defined in either Ext or other js files that I load after Ext.

After some trial and error, I put console.log()s at the end of few js files and none shows up. I think using ext-all-debug.js somehow causes the js code in <BODY></BODY> to be executed before the scripts in the <HEAD> are fully loaded and this results in "undefined" errors because the code in the <BODY> references values that are supposed to be loaded beforehand.

right ?

<HEAD>
<SCRIPT type="text/javascript" src="ext/ext-base.js"></SCRIPT>
<SCRIPT type="text/javascript" src="ext/ext-all-debug.js"></SCRIPT>
<SCRIPT type="text/javascript" src="my_lib.js"></SCRIPT> <!-- defines myValue -->
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
console.log(myValue) // undefined
</SCRIPT>
</BODY>

here is my_lib.js

var myValue = 10;
console.log("my_lib.js loaded");

And

my_lib.js loaded

does not show up in the console.

解决方案

As long as scripts aren't marked defer or async or loaded dynamically, they will execute in the order that they are encountered in the HTML file. That is part of the spec so it's guaranteed.

If you are seeing myValue as undefined after you think you've defined it in an earlier script, then there is some other problem.

Some possibilities are:

  1. One of your scripts has an error that prevents full execution.
  2. Your variable isn't getting defined in the global scope, but rather some sub-scope so it isn't available at the global scope when you try to use it.
  3. Your variable is defined by the previous script, but not successfully initialized to have a value other than undefined.
  4. Your scripts aren't being found by the browser (and thus not loading/executing).

If you can show us a working/non-working page that exhibits the problem, we can likely spot the issue quickly. As it is now with no sample page that shows the issue, we can only speculate on possible causes.

这篇关于其中的Javascript代码&lt; BODY&gt;在*&lt; SCRIPT&gt;之前运行*&lt; HEAD&gt;满载了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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