文件已经澄清了吗? [英] Document.ready clarification?

查看:64
本文介绍了文件已经澄清了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到一些使用此模式的网站:

I lately saw some sites which uses this pattern :

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

  <script> 
    $(function (){...do some stuff with plugins...});
  </script>

</head>
<body>


  <script src="myplugin1.js"></script>
  <script src="myplugin2.js"></script>
  <script src="myplugin3.js"></script>
</body>
</html>

这让我想到了一些陷阱:

This made me think of some traps :

问题#1

document.ready 事件引发后未执行插件(JS)已被解析。

document.ready event is raised not executed after the plugins(JS) has been parsed.

当dom 结构完成时执行。 (注意:我没有说:当所有资源都已下载时!)

it is executed when the dom structure is done. (notice: I didn't say :"when all resources has been downloaded" !)

因此可能存在文件。 ready 函数将尝试使用尚未完全下载的插件变量。 (这会导致错误)。

So there could be a situation where the document. readyfunction will try to use a plugin variable which hasn't been fully downloaded. (which will cause error).

我是对的吗?

问题#2
这导致我:从不使用document.ready之前脚本引用位置(我的意思是:在document.ready依赖于那些脚本变量的情况下)。

Question #2 This leads me to : "never use document.ready" before the script references location ( I mean : in situations where document.ready is dependent on those script variables).

我是对的吗?

ps我不是在谈论window.load,这显然会在这里工作,但我必须等待更长时间。

推荐答案

如果您考虑页面中的所有类型的资源,可以将许多资源与页面内容分开加载:例如,图像和样式表。它们可能会改变页面的外观,但它们无法真正改变结构,所以单独加载它们是安全的。

If you think about all the kinds of resources in a page, many can be loaded separately from the page content: images and stylesheets, for example. They may change the look of the page, but they can't really change the structure, so it's safe to load them separately.

另一方面,脚本有这个名为 document.write 的小东西可以让工作变得棘手。如果我有这样的HTML:

Scripts, on the other hand, have this little thing called document.write that can throw a wrench into the works. If I have some HTML like this:

Who would <script>document.write("<em>");</script>ever</em> write like this?

然后浏览器会解析它就好了; document.write ,如果在顶层使用,则有效地在要解析的位置插入HTML。这意味着页面的其余部分取决于脚本元素,因此在该脚本加载之前,浏览器无法真正继续使用该文档。

Then browsers will parse it just fine; document.write, if used at the top level like that, effectively inserts HTML at that position to be parsed. That means that the whole rest of the page depends upon a script element, so the browser can't really move on with the document until that script has loaded.

因为脚本可能会修改HTML并更改结构,浏览器不能等到以后加载它们:它必须在那时加载它们,并且它不能说DOM已准备就绪,因为脚本可能会修改它。 因此,浏览器必须延迟DOM就绪事件,直到所有脚本都运行完毕。这使您可以安全地将相关代码放在就绪处理程序的顶部。

Because scripts can potentially modify the HTML and change the structure, the browser can't wait to load them later: it has to load them at that moment, and it can't say the DOM is ready yet because a script might modify it. Therefore, the browser must delay the DOM ready event until all the scripts have run. This makes it safe for you to put the dependent code at the top in a ready handler.

但是,我建议你不要,因为它不是很清楚。

However, I'd recommend you don't, because it's not very clear.

这篇关于文件已经澄清了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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