当同一 HTML 页面上使用的两个 JavaScript 文件中有两个 jQuery $(document).ready 调用时会发生什么?

发布于 2024-11-16 17:17:54 字数 838 浏览 3 评论 0 原文

我有一个关于 jQuery $(document).ready 的问题

假设我们有一个 HTML 页面,其中包含 2 个 JavaScript 文件

<script language="javascript" src="script1.js" ></script>
<script language="javascript" src="script2.js" ></script>

现在假设在这两个脚本文件中,我们都有 < code>$(document) 如下

script1.js 内部:

$(document).ready(function(){
    globalVar = 1;
})

script2.js 内部:

$(document).ready(function(){
    globalVar = 2;
})

现在我的问题是:

  1. 这两个就绪事件函数都会被触发吗?
  2. 如果是,他们被解雇的顺序是什么? 文件将同时准备好 他们俩都有时间吗?
  3. 是否推荐使用这种方法,或者我们理想情况下应该只有 1 个 $(文档).准备好了吗?
  4. 所有浏览器(IE、FF 等)的执行顺序是否相同?

谢谢。

I have a question on jQuery $(document).ready

Let's say we have a HTML page which includes 2 JavaScript files

<script language="javascript" src="script1.js" ></script>
<script language="javascript" src="script2.js" ></script>

Now let's say in both these script files, we have $(document) as follows

Inside script1.js:

$(document).ready(function(){
    globalVar = 1;
})

Inside script2.js:

$(document).ready(function(){
    globalVar = 2;
})

Now my Questions are:

  1. Will both these ready event function get fired ?
  2. If yes, what will the order in which they get fired, since the
    document will be ready at the same
    time for both of them?
  3. Is this approach recommended OR we should ideally have only 1
    $(document).ready ?
  4. Is the order of execution same across all the browsers (IE,FF,etc)?

Thank you.

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

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

发布评论

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

评论(5

请叫√我孤独 2024-11-23 17:17:54
  1. 这两个就绪事件函数都会被触发吗?

是的,他们都会被解雇。

  • 由于文件将同时为他们两人准备好,因此他们被解雇的顺序是什么?
  • 它们出现的方式(从上到下)是因为就绪事件将被触发一次,并且所有事件侦听器将相继收到通知。

  • 是否建议采用这种方法,或者理想情况下我们应该只有 1 $(document).ready ?
  • 这样做就可以了。如果可以将它们放在同一个块代码中,管理起来会更容易,但仅此而已。 更新:显然我忘了提及,如果您在多个文件中执行此操作,则会增加 JavaScript 代码的大小。

  • 所有浏览器(IE、FF 等)的执行顺序是否相同?
  • 是的,因为 jQuery 实现了跨浏览器标准化。

    1. Will both these ready event function get fired ?

    Yes, they will both get fired.

    1. what will the order in which they get fired, since the document will be ready at the same time for both of them?

    In the way they appear (top to bottom), because the ready event will be fired once, and all the event listeners will get notified one after another.

    1. Is this approach recommended OR we should ideally have only 1 $(document).ready ?

    It is OK to do it like that. If you can have them in the same block code it would be easier to manage, but that's all there is to it. Update: Apparently I forgot to mention, you will increase the size of your JavaScript code if you do this in multiple files.

    1. Is the order of execution same across all the browsers (IE,FF,etc)?

    Yes, because jQuery takes the cross-browser normalization at hand.

    没有伤那来痛 2024-11-23 17:17:54

    请参阅此处: jQuery - 拥有多个 $(document ).ready(function() {}); 和此处: 教程:多个 $(document).ready()

    1. 是附加顺序
    2. jQuery 内部 维护一个带有 readyList ="http://api.jquery.com/category/deferred-object/" rel="nofollow noreferrer">延迟对象
    3. 这部分是一个品味问题。拥有一个就绪处理程序将使您对正在发生的所有事情有一个很好的概述,而多个(即每个包含的文件一个)将使您的代码更加模块化(即,您可以包含或删除 .js 文件,并确保它提供并绑定自己的就绪处理程序)。
    4. 是的 - 附加顺序。

    See here: jQuery - is it bad to have multiple $(document).ready(function() {}); and here: Tutorials:Multiple $(document).ready()

    1. Yes
    2. Order of attach. jQuery internally maintains a readyList with Deferred objects.
    3. It's partially a matter of taste. Having one ready handler will give you a nice overview of all that is happening, while multiple (i.e., one per included file) will make your code much more modular (i.e., you can include or remove a .js file and be sure that it provides and binds its own ready handler).
    4. Yes - order of attach.
    故事未完 2024-11-23 17:17:54

    在任何当前浏览器中,您可以指望两个处理程序都按照脚本包含的顺序执行,并且在第二个脚本引用之后 globalVar2

    You can count on both handlers being executed in order of their script inclusion and globalVar being 2 after the second script reference, in any current browser.

    执笔绘流年 2024-11-23 17:17:54

    如果您想要完全控制,我强烈建议仅一个 $(document).ready();

    如果您通过 ajax 加载 HTML 的部分部分,并且 ajax 响应包含 $(document).ready(); 脚本,并且您想要触发 $(document).ready();来自 ajax 回调中的 script1.js、script2.js 等。您必须复制大量代码...

    祝您好运!

    / $(窗口).ready(); ;)

    If you want full control I strongly recommend only one $(document).ready();

    If you load partial portions of HTML through ajax and the ajax response includes a $(document).ready();-script and you want to fire $(document).ready(); from script1.js, script2.js and so on in the ajax callback.. You have to duplicate PLENTY of code....

    Good Luck!

    / $(window).ready(); ;)

    自找没趣 2024-11-23 17:17:54
    1. 两者都会被解雇
    2. 。一旦尘埃落定,变量的值将为 2。
    3. 不建议的主要事情是使用 2 个不同的 JS 文件,正如 Google PageSpeed 和 Yahoo YSlow 所建议的那样,最好将所有 Javascript 代码放在同一个文件中。
      就相同的事件处理程序而言,老实说,我认为没有理由这样做,而且这只会让您的代码可读性更差。
    4. 对此我没有答案。
    1. Both will get fired
    2. The value of the variable will be 2 once all the dust has settled.
    3. The main thing which isn't recommended is using 2 different JS files, as Google PageSpeed, and Yahoo YSlow recommends, it's best to have all your Javascript codes in the same file.
      as far as same event handlers, well, in all honesty, I see no reason why to do that, and it'll only make your code readability lousier.
    4. I have no answer for that.
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文