Modernizr 与 $(document).ready()
从网络面板看来,当您通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是这样看的:使用 Modernizr(和浏览器测试)加载 Javascript 文件是一个非常通用的设计决策,团队中的每个人在任何情况下都需要注意。
但是,您可能希望(如果还没有)对应用程序进行某种初始化,这应该是在加载所有内容之后最后运行的事情。然后可以在 yepnope 回调中执行此初始化,显然甚至在
$(document).ready()
包装器中也是如此。Yepnope 在“常见问题”中指出:
为了完整起见:
无需进一步检查,这里有一些关于如何处理
document.ready + Modernizr.load
的其他想法:如何将 yepnope.js 与 $(document).ready() 一起使用有效吗?和https://stackoverflow.com/a/6767886/564721I'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":
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