简体   繁体   English

在$(document).ready()之前运行一个函数

[英]Running a function just before $(document).ready() triggers

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. 我已将多个文件中的多个函数附加到$(document).ready,并希望在它们之前附加一个函数作为$(document).ready处理的第一个函数或者在$(document)之前独立触发的函数.ready处理程序。

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. 有没有办法处理jQuery在jQuery.fn.ready中内部触发的函数的顺序,或挂钩在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) 在第三方脚本中编辑jQuery.fn.ready是安全的,或者它会在其他第三方插件中引起可怕的敲击效果(除了编辑jQuery.fn.ready本身的插件)

[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. 函数a首先需要与订单无关,但只能在几页上调用。 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. 我不能保证b或c的顺序,所以我不能在b或c的开头触发自定义事件来触发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. 所以我想要一个通用的解决方案,我可以用它来强制放置一个jQuery的内部readyList的开头或挂钩到jQuery的ready函数并安全地编辑它,因此它在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(). 如果可能的话,我宁愿不必重新设计javascript代码执行顺序,或者除了函数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() {});

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. 这将是我将触发所有其他文件将bind到的自定义事件的时间,您将只有一个ready处理程序,它将执行操作,然后触发自定义事件。

So, somewhere else in your code, instead of using $(document).ready , you would use 所以,代码中的其他地方,而不是使用$(document).ready ,你会使用

$(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: 在你唯一的ready处理程序中,你会做这样的事情:

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

$(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()或者只是$(function(){})只是一个.addEventListener实现(不完全,但是很接近),这意味着你可以在之前和之后添加监听器。

$(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. 没有必要破解.ready() imo。

I just had the exact same problem. 我刚才遇到了同样的问题。 I put a tiny script on GitHub that adds a $.beforeReady() function. 我在GitHub上添加了一个小脚本,它添加了一个$ .beforeReady()函数。 Funny :) 有趣:)

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

Just load the script right after jQuery like in this example: 只需在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM