I'm curious what situations exactly require the use of jquery's $(document).ready() or prototype's dom:loaded or any other variant of a handler for this event.

我很好奇什么情况确实需要使用jquery的$(document).ready()或prototype的dom:loaded或者此事件的处理程序的任何其他变体。

In all the browsers i've tested, it's entirely acceptable to begin interacting with html elements and the DOM immediately after the closing tag. (e.g.

在我测试的所有浏览器中,在结束标记之后立即开始与html元素和DOM进行交互是完全可以接受的。 (例如。

<div id="myID">
 My Div
</div>
<script type="text/javascript">
$('#myID').initializeElement();
</script>

So at this point i'm wondering whether $(document).ready() is merely there to reduce the thinking involved in writing javascript code that runs during page load. In the case of using $(document).ready() there is regularly rendering issues such as popping and 'artifacts' between the browser first starting to draw the page and the javascript actually executing when the page is 'ready'.

所以在这一点上我想知道$(document).ready()是否只是为了减少编写在页面加载期间运行的javascript代码所涉及的思路。在使用$(document).ready()的情况下,在浏览器首先开始绘制页面和javascript实际执行页面“准备好”之间经常出现渲染问题,例如弹出和“工件”。

Are there any scenarios where $(document).ready() is required?

有没有需要$(document).ready()的场景?

Are there any reasons I shouldn't be writing initialization scripts as demonstrated?

是否有任何理由我不应该编写初始化脚本?

3 个解决方案

#1


10

The main reason is external files that are included in the head. When you include a file in your <head> it gets run immediately. This means if the JavaScript interacts with the DOM it needs to be wrapped in Dom Ready.

主要原因是头部包含的外部文件。当您在中包含文件时,它会立即运行。这意味着如果JavaScript与DOM交互,则需要将其包装在Dom Ready中。

It's also needed for unobtrusive JavaScript and separations of concerns. Ideally your JavaScript and HTML are in separate files. If you follow this you will not have any in-line script tags in your HTML at all.

它也需要不引人注目的JavaScript和关注点的分离。理想情况下,您的JavaScript和HTML位于不同的文件中。如果您按照此操作,则HTML中根本不会包含任何内嵌脚本标记。

The second is to defend yourself against obscure browser bugs when you make mistakes. There are cases where it's not save to go and manipulate DOM elements immediately afterwards. (I'm looking at you IE6!)

第二个是在出错时保护自己免受不明确的浏览器错误的侵害。有些情况下,以后不会立即去操作DOM元素。 (我在看着你的IE6!)

The third reason is to keep your code clean.

第三个原因是保持代码清洁。

Mixing script tags into your HTML makes spaghetti code.

将脚本标记混合到HTML中会产生意大利面条代码。

some HTML
<script> ... </script>

some HTML
<script> ... </script>

some HTML
<script> ... </script>

some HTML
<script> ... </script>

some HTML
<script> ... </script>

some HTML
<script> ... </script>

some HTML
<script> ... </script>

We have code about ten times worse then that. It's a right pain to read / maintain

我们的代码差了十倍。阅读/维护是一种正确的痛苦

更多相关文章

  1. Jquery禁用/启用按钮与文本框代码
  2. 推荐web 前端代码的编辑分享平台——RunJS
  3. 想要改进我的jQuery代码,做我想要的,太丑了,IMO
  4. JQuery的一些基本功能代码(CH1&2)
  5. 如何让代码等待好/取消按钮选择?
  6. 如何在angularjs代码中单元测试jquery元素
  7. jquery常用代码片段
  8. Webhoster插入了一个javascript,它会阻止我的代码如何删除它?
  9. 可以直接拿来用的15个jQuery代码片段

随机推荐

  1. Android和设计模式:享元模式
  2. Android 自定义组合控件
  3. Android中webview拨打加载网页中的电话超
  4. Android(安卓)压缩工具类
  5. Android调用WebService系列之封装能力
  6. Android与Js通信之JsBridge再封装
  7. 2016这一年,回顾我们一起走过的"编码美丽"
  8. Android(安卓)RIL log全记录
  9. Android版本管理解决方法小议
  10. 获取Android自己写好了的apk以及反编译