在 $(document).ready() 触发之前运行函数
我已将多个文件中的多个函数附加到 $(document).ready ,并希望附加一个函数在它们之前发生,作为 $(document).ready 处理的第一个函数或在 $(document) 之前独立触发.ready 处理程序。
有什么方法可以处理 jQuery 作为 jQuery.fn.ready 的一部分在内部触发的函数的顺序,或者挂钩在 jQuery.fn.ready 之前调用的函数。
在第 3 方脚本中编辑 jQuery.fn.ready 是否安全,或者是否会在其他第 3 方插件中造成可怕的连锁反应(除了编辑 jQuery.fn.ready 本身的插件)
[编辑]:
作为示例
$(document).ready(function b() {});
....
$(document).ready(function a() {/* optional setup*/});
....
$(document).ready(function c() {});
函数 a需要首先发生,与顺序无关,但只会在几页上被调用。我无法保证 b 或 c 的顺序,因此无法在 b 或 c 的开头触发自定义事件来触发 a。
所以我想要一个通用的解决方案,我可以用它来强制将 a 放置在 jQuery 的内部 readList 的开头,或者挂钩到 jQuery 的 read 函数并安全地编辑它,以便它在readyList 中的任何其他函数之前调用 a 。
[进一步编辑]
如果可能的话,我宁愿不必重新设计 javascript 代码执行顺序,也不必接触除函数 a() 之外的任何其他代码。我知道重组可以让我解决这个问题,但我宁愿只编写一个函数,例如
$.beforeReady(function a() {});
I've attached multiple functions in multiple files to $(document).ready and would like to attach a single function to happen before them as either the first that $(document).ready handles or to independently trigger before the $(document).ready handler.
Is there any way to handle the order of functions that jQuery triggers internally as part of jQuery.fn.ready or to hook in a function that calls just before jQuery.fn.ready.
Is editing jQuery.fn.ready in a 3rd party script safe to do or will it cause horrible knock on effects in other 3rd party plugins (apart from plugins that edit jQuery.fn.ready themselves)
[Edit]:
as an example
$(document).ready(function b() {});
....
$(document).ready(function a() {/* optional setup*/});
....
$(document).ready(function c() {});
function a needs to happen first irrelevant of order, but only gets called on a few pages. I cant guarantee the order of b or c so I cant trigger custom events at the start of b or c to trigger a.
So I would like a generic solution that I can use to forcefully place a at the start of jQuery's internal readyList or hook into jQuery's ready function and edit it safely so it calls a before any of the other functions in readyList.
[Further Edit]
If possible I would rather not have to redesign the javascript code execution order or have to touch any other code apart from function a(). I'm aware restructuring would allow me to get around this problem but I'd rather just write a single function like
$.beforeReady(function a() {});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这时候我会触发一个自定义事件,所有其他文件都会
绑定
到该事件,您只有一个ready
处理程序,它会做一些事情,然后触发自定义事件。因此,在代码的其他地方,您可以
使用And/or
在唯一的
ready
处理程序中 And,而不是使用$(document).ready
做这样的事情:This would be a time where I would trigger a custom event that all of your other files would
bind
to, you would only have oneready
handler, which would do stuff, then trigger the custom event.So, somewhere else in your code, instead of using
$(document).ready
, you would useAnd/or
And in your one and only
ready
handler, you'd do something like this:$(document).ready()
或简单的$(function(){})
只是一个.addEventListener
实现(不完全是,但关闭),这意味着您可以在之前和之后添加侦听器。等等,将函数夹在中间并不难。
在我看来,没有必要破解
.ready()
。$(document).ready()
or simply$(function(){})
is just an.addEventListener
implementation (well not exactly, but close), meaning you can add listeners before and after.And so on, it's not hard to sandwich functions.
There's no need to hack
.ready()
imo.我刚刚遇到了完全相同的问题。我在 GitHub 上放了一个小脚本,添加了 $.beforeReady() 函数。有趣:)
https://github.com/aim12340/jQuery-Before-Ready
只需在 jQuery 之后加载脚本,如下例所示:
I just had the exact same problem. I put a tiny script on GitHub that adds a $.beforeReady() function. Funny :)
https://github.com/aim12340/jQuery-Before-Ready
Just load the script right after jQuery like in this example: