Is $(document).ready necessary?

前端 未结 5 2056
太阳男子
太阳男子 2020-11-22 15:07

I saw this question in stackoverflow but do not feel that it was answered at all.

Is $(document).ready necessary?

I link all my javascripts at t

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 16:00

    No, it isn't necessary provided you know you do not have any deferred stuff happening-- and in most cases you will know if you have developed what you are working on from top to bottom.

    --It is when you bring in someone else's code, without thoroughly auditing it, that you don't know.

    So, ask yourself are you using a framework or editor that helps you with the structure? Are you bringing in someone else's code and you haven't bothered to read through each file? Are you prepared to go through the Operating System, Browser, and Browser Version matrix of testing your code? Do you need to squeeze every single ounce of speed from your code?

    document.ready() makes many of those question become irrelevant. document.ready() was designed to make your life easier. It comes at a small (and I dare say acceptable) performance hit.

提交回复
热议问题