Ext.onReady() vs $(document).ready()

后端 未结 3 2277
梦如初夏
梦如初夏 2021-02-15 11:45

Whats the difference? I have on $(document).ready function which should check if extjs is loaded but the main problem is extjs does not load on time and things inside $(document

3条回答
  •  逝去的感伤
    2021-02-15 12:22

    Ext.onReady() and $(document).ready() have nothing to do about either library being loaded as the current accepted answer suggests.

    According to the documentation both are about the DOM being loaded and ready.

    Documentation

    • Ext JS: https://docs.sencha.com/extjs/6.7.0/modern/Ext.html#method-onReady
    • jQuery: https://api.jquery.com/ready/

    An Answer to Your Case

    It's possible that you're loading the Ext JS resource after your script fires, but jQuery is already loaded above your script. Thus using jQuery to wait until the DOM is loaded guarantees that the DOM has been loaded and thus by then Ext JS has also been loaded.

    If you try to invert them and us Ext JS first you'll likely have an error.

    According to the documentation they're doing the same thing so you shouldn't need to nest them

    A Fix for this Scenario

    If you are loading your resources like so:
    1. jQuery
    2. Your Script
    3. Ext JS
    It would be best to load them in this order:
    1. jQuery and/or Ext JS
      • Order shouldn't matter as they can stand by themselves without requiring one or the other
    2. Your Script

    Additional Explanation

    Due to how the DOM is loaded and parsed by the time it reads your script it guarantees that jQuery and Ext JS are available. This is why you can reference their libraries in your script; you're not waiting for them to load they're already there and available to be used which is why you can call them and use their ready calls.

    You need to use the ready event of one of the libraries to guarantee that all elements are loaded into the DOM and available to be accessed. You also shouldn't try to add anything to the DOM until it's ready although you can append to current elements that have been loaded above your element/script tag. It's just best practice to not touch the DOM until it's finished loading.

    Additional Explanation Nobody Asked For

提交回复
热议问题