Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of loading external CSS and JavaScript files

I have a third party application that loads many css and javascript files, and I now want to optimize this by concatinating all the javascripts into one file, compressed by the yuicompressor, but ... when we have a mix like:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<link rel="stylesheet" href="style1.css" type="text/css" />
<script type="text/javascript" src="script3.js"></script>
<script type="text/javascript" src="script4.js"></script>

Does it matter that there's a css in the middle here? Should I concatinate and yuicompress the 4 javascripts and load them before the CSS or after?

like image 796
janfrode Avatar asked Nov 16 '10 20:11

janfrode


People also ask

Which loads first CSS or JavaScript?

When it comes to ordering your CSS and JavaScript, you want your CSS to come first. The reason is that the rendering thread has all the style information it needs to render the page. If the JavaScript includes come first, the JavaScript engine has to parse it all before continuing on to the next set of resources.

What order are CSS files loaded?

Generally the last rule takes precedence. With that being said, there are "exceptions" in that inline styles take precedence over external stylesheets ( an inline ! important is more important than an external ! important, etc ), and more specific selectors override generic selectors.

Does HTML or JavaScript load first?

The browser loads the html (DOM) at first. The browser starts to load the external resources from top to bottom, line by line. If a <script> is met, the loading will be blocked and wait until the JS file is loaded and executed and then continue.

How do I organize my HTML CSS and JavaScript files?

One approach is to place HTML files inside a folder, CSS files inside a folder and javascript file inside a js folder and include the javascript files from the js folder into the main HTML page.


1 Answers

Check out Yahoo's Best Practices for Speeding Up Your Web Site, they recommend loading your css first (preferably in the header), and your js last (after all the content in the body). Google's best practices also recommended loading CSS first.

like image 142
Sebastian Patane Masuelli Avatar answered Oct 19 '22 23:10

Sebastian Patane Masuelli