views:

229

answers:

5

I'm building up my site over at http://royronalds.com, and I'm trying to figure out what order of elements in the <head> makes most sense. Just to take from what I currently have, I have:

  • <head>
  • <style> external stylesheet
  • <meta>
  • <title>
  • <link> to favicon
  • <script> for jQuery
  • <script> main javascript for site
  • <script> google analytics, asynchronous script.
  • </head>

Are there reasons to order these differently so that load times and other issues happen more smoothly, and if so, what would the ideal order be?

+5  A: 

One thing that can have a big effect is moving <script> tags to the bottom of the page if they are not essential to the content.

For instance, I would move the google analytics <script> tag to be the last thing before the </body> tag on every page. Script tags are "blocking content", so the browser will not continue with a page render until the script has been downloaded and executed. Your main javascript and JQuery files probably can't be moved as easily depending on how you're using them, but analytics for sure can be at the bottom.

Check out the YSlow best practices for more optimization techniques.

zombat
Asynchronous script tags like the Google Analytics asynchronous tracking snippet do not block content like normal scripts do. Please see:http://www.stevesouders.com/blog/2010/03/03/p3pc-google-analytics/
Brian
Interesting. That's a relatively recent change to the GA code. I wasn't aware of it, thanks for the link. Most sites are probably still using the old non-asynchronous script from the sounds of it.
zombat
Most sites do, and your comment definitely applies to the non-asynchronous GA snippet. I just wanted to point out that this question was asking about async GA.
Brian
+3  A: 

Modern browsers wait with any sort of rendering until the entire <head> section is retrieved (including the files linked within it). The order therefore doesn't matter for performance. For Javascript, the order of the files is the order of execution. For stylesheets, the order determines precedence (the rule that was defined last has precedence if all other things are equal).

If you want to optimise client performance, it is strongly advisable to move your Javascript includes to the very end of the <body> element, instead of putting them in the <head> at all. There are more considerations, Yahoo's list of optimisations is well worth your time to read through. Google has some good advice as well.

molf
The execution order for asynchronous scripts is not necessarily the order in which they appear on the page. It's also not necessarily advisable to put asynchronous scripts at the bottom. On many pages, putting async scripts near the top can lower the total page load time without blocking content due to increased parallelism.
Brian
A: 

Also consider the fact that browsers will halt javascript execution when an error occurs.

Because of this, it is very important that your javascript work on all browsers else you risk your Analytics code not executing and capturing stats...

I usually place my Google Analytics above my own code so it runs first, and if in the rare event that my code breaks or causes JS errors, I am still able to get tracking information.

my order would be:

<head>
<meta> content encoding
<title>
<link> favicon
<style> external/third-party stylesheet
<style> site stylesheet
</head>
<body>
...
<script> google analytics
<script> jQuery
<script> jQuery plugins
<script> site javascript
</body>
farinspace
Browsers only stop script execution for the script tag where the error occurs. They will continue to execute code in other scripts. Having a broken script above the GA script will not stop tracking beacons from being sent.
Brian
+1  A: 

by the way, try to antispam your email address...

ie:

<SCRIPT TYPE="text/javascript">
document.write('<A HREF=' + '"mai' + 'lto:' + 'example@' + 'example' + '.fr' + '?' + 'sub' + 'ject=music'+'example'+'text">' + 'example' +'@' + 'example' +'.' + 'fr' + '</A><BR>')
</SCRIPT>

It works for me...

The HCD
Yeah, I'll try to think of something (preferrably something more accessible) for that.
Tchalvak
A: 

If you are using Wordpress then you can easily use this plugin: http://wordpress.org/extend/plugins/performance-optimization-order-styles-and-javascript/ and take advantage of it.

Satya Prakash