最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript and CSS order - Stack Overflow

programmeradmin5浏览0评论

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 there any way to change the order ?

Thanks !

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 there any way to change the order ?

Thanks !

Share Improve this question asked Apr 13, 2010 at 15:27 Misha MoroshkoMisha Moroshko 171k229 gold badges520 silver badges760 bronze badges 2
  • 2 I think that depends heavily on the browser and the structure of the document. Why are you asking this? How are your JavaScript file and your style sheet connected? – Pekka Commented Apr 13, 2010 at 15:30
  • Refer to this answer at stackoverflow./a/1795502/1074998 , it's executed in top-down order, regardless of css or js. – 林果皞 Commented Oct 20, 2017 at 17:43
Add a ment  | 

3 Answers 3

Reset to default 8

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 ponents 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.

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.

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./performance/rules.html

发布评论

评论列表(0)

  1. 暂无评论