Requirejs domReady plugin vs Jquery $(document).ready()?

后端 未结 5 1149
野性不改
野性不改 2020-12-07 07:29

I am using RequireJS and need to initialize something on DOM ready. Now, RequireJS provides the domReady plugin, but we already have jQuery\'s $(document).ready()

5条回答
  •  温柔的废话
    2020-12-07 08:00

    Answering your bullets in order of appearance:

    • They both do accomplish the same thing
    • If you have reservations about jquery for whatever reason then use domReady
    • Correct, so just use jQuery
    • Because not everyone uses jQuery
    • I agree, just use jQuery
    • Plugins by definition 'feed a need'.
    • Cross Browser ajax isn't a thing. Cross Domain? There probably is, and if there isn't then there is no need to feed.
    • , -, -, - Ok

    When it comes down to it, you are overthinking this. It's a mechanism to execute javascript on domReady. If you didn't have jQuery I would advocate the domReady plugin. Since you have jQuery then don't load more scripts to do something that is already available.

    Clarity Update

    The domReady plugin collects functions to call when the document is 'ready'. If it is already loaded then they execute immediately.

    JQuery collects functions and binds a deferred object to the dom being 'ready'. When the dom is ready the deferred object will be resolved and the functions will fire. If the dom is already 'ready' then the deferred will already be resolved so the function will execute immediately.

    This means that effectively they do exactly the same thing.

提交回复
热议问题