JavaScript and CSS order

后端 未结 3 1911
無奈伤痛
無奈伤痛 2021-01-13 19:42

I have an HTML file which is linked to CSS file and also to JavaScript file.

Is JavaScript executed first and then the CSS is applied, or vice versa ?

Is the

相关标签:
3条回答
  • 2021-01-13 19:58

    It's generally considered a good idea to import your scripts as late as possible, and your stylesheets as early as possible. If possible, in fact, you should stick all your script imports at the very end of the <body>. I find that problematic when there are components pulled into the page that want to be able to drop little script blocks that reference jQuery (for example).

    If your stylesheets are first, that helps make sure the browser applies styles before showing anything to the user. Conversely, by including scripts last, you defer that potentially slow script processing until after the point where the user gets to see something on the screen.

    0 讨论(0)
  • 2021-01-13 20:08

    The JavaScript gets executed when the <script> element is parsed. Some of the JS might set up event handlers to run some JS when events happen.

    CSS is applied to the live DOM. Changes to the DOM get CSS applied automatically. Changes to CSS apply to the whole DOM automatically.

    0 讨论(0)
  • Yahoo's research into speeding-up page loading times should be very helpful, and they explain things much clearer than I can.

    http://developer.yahoo.com/performance/rules.html

    0 讨论(0)
提交回复
热议问题