在 $(document).ready() 触发之前运行函数

发布于 2024-09-30 11:55:56 字数 876 浏览 4 评论 0原文

我已将多个文件中的多个函数附加到 $(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 技术交流群。

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

发布评论

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

评论(3

悲歌长辞 2024-10-07 11:55:56

这时候我会触发一个自定义事件,所有其他文件都会绑定到该事件,您只有一个ready处理程序,它会做一些事情,然后触发自定义事件。

因此,在代码的其他地方,您可以

$(window).bind('setup', function() {
   //code here...
});

使用And/or

$(window).bind('loaded', function() {
   //some other code here...
});

在唯一的 ready 处理程序中 And,而不是使用 $(document).ready做这样的事情:

$(document).ready(function() {
   $(window).trigger('setup');
   $(window).trigger('loaded'):
});

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 one ready handler, which would do stuff, then trigger the custom event.

So, somewhere else in your code, instead of using $(document).ready, you would use

$(window).bind('setup', function() {
   //code here...
});

And/or

$(window).bind('loaded', function() {
   //some other code here...
});

And in your one and only ready handler, you'd do something like this:

$(document).ready(function() {
   $(window).trigger('setup');
   $(window).trigger('loaded'):
});
沙沙粒小 2024-10-07 11:55:56

$(document).ready() 或简单的 $(function(){}) 只是一个 .addEventListener 实现(不完全是,但关闭),这意味着您可以在之前和之后添加侦听器。

$(document).ready(listener); // goes off first.
$(document).ready(otherListener); // goes off 2nd.

等等,将函数夹在中间并不难。
在我看来,没有必要破解 .ready()

$(document).ready() or simply $(function(){}) is just an .addEventListener implementation (well not exactly, but close), meaning you can add listeners before and after.

$(document).ready(listener); // goes off first.
$(document).ready(otherListener); // goes off 2nd.

And so on, it's not hard to sandwich functions.
There's no need to hack .ready() imo.

烟凡古楼 2024-10-07 11:55:56

我刚刚遇到了完全相同的问题。我在 GitHub 上放了一个小脚本,添加了 $.beforeReady() 函数。有趣:)

https://github.com/aim12340/jQuery-Before-Ready

只需在 jQuery 之后加载脚本,如下例所示:

<html>
    <head>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="jquery-before-ready.js"></script>
    </head>
    <body>
        <script>
            $(document).ready({
                alert("This function is declared first!")
            })
        </script>
        <script>
            $.beforeReady(function(){
                alert("This function is declared second but executes first!")
            })
        </script>        
    </body>
</html>

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:

<html>
    <head>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="jquery-before-ready.js"></script>
    </head>
    <body>
        <script>
            $(document).ready({
                alert("This function is declared first!")
            })
        </script>
        <script>
            $.beforeReady(function(){
                alert("This function is declared second but executes first!")
            })
        </script>        
    </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文