Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript and CSS order

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 !

like image 395
Misha Moroshko Avatar asked Apr 13 '10 15:04

Misha Moroshko


2 Answers

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.

like image 177
Pointy Avatar answered Oct 01 '22 11:10

Pointy


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.

like image 30
Quentin Avatar answered Oct 01 '22 11:10

Quentin