Modernizr 与 $(document).ready()

发布于 2024-12-23 12:08:20 字数 216 浏览 5 评论 0原文

从网络面板看来,当您通过Modernizr.load加载脚本时,它实际上并没有阻止$(document).ready()。

所以从理论上讲,假设我通过 Modernizr 添加 json2.js,而另一个开发人员尝试在 $(document).ready() 中使用 JSON.parse,他们的脚本实际上可能会因为竞争条件而中断。

是否可以在不尝试监管所有代码签入的情况下解决此问题?

It appears from network panel, when you load scripts through Modernizr.load, it doesn't actually blocks $(document).ready().

So theoretically, let's say that I add json2.js through Modernizr, and another developer tries to use JSON.parse in $(document).ready(), their script could actually break because of the race condition.

Would it be possible to remedy this without try to police all the code check-ins?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2024-12-30 12:08:21

我是这样看的:使用 Modernizr(和浏览器测试)加载 Javascript 文件是一个非常通用的设计决策,团队中的每个人在任何情况下都需要注意。

但是,您可能希望(如果还没有)对应用程序进行某种初始化,这应该是在加载所有内容之后最后运行的事情。然后可以在 yepnope 回调中执行此初始化,显然甚至在 $(document).ready() 包装器中也是如此。

Yepnope 在“常见问题”中指出:

仅仅因为您的脚本已完成,并不意味着文档已准备就绪。
不要忘记您可以在您的内部使用文档就绪回调
是的回调
。如果你正在玩弄 DOM,我们会大力
鼓励您这样做,因为您的测试环境可能会起作用
与生产服务器不同,速度非常快
不同。


为了完整起见:

无需进一步检查,这里有一些关于如何处理 document.ready + Modernizr.load 的其他想法:如何将 yepnope.js 与 $(document).ready() 一起使用有效吗?https://stackoverflow.com/a/6767886/564721

I'd see it this way: Using Modernizr (and browser tests) to load Javascript files is a very general design decision everyone on your team needs to be aware of in any case.

However, you probably want to implement (if you haven't yet) some sort of initialization of your application, which should be the last thing to be run — after everything has been loaded. This initialization can then be executed in a yepnope callback, apparently even within a $(document).ready() wrapper.

Yepnope states in this context in the "common gotchas":

Just because your script is done, doesn't mean the document is ready.
Don't forget that you can use document ready callbacks inside of your
yepnope callbacks
. If you're toying with the DOM, we'd heavily
encourage you to do so, because your test environment may act
differently than your production server the speeds are dramatically
different.


And for the sake of completeness:

Without checking them any further, here a couple of other ideas on how to deal with document.ready + Modernizr.load: How can I use yepnope.js with $(document).ready() effectively? and https://stackoverflow.com/a/6767886/564721

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文