Put script after content

By default, we should put the script tag after our content. It usually means putting them just before the closing of the body tag.

The reason is that browser stops loading when it encounters a script tag. If we put the script after content, we let all the content loads and displays before we pause the loading for the script tag. Moreover, we usually access the HTML elements inside our script. This means wherever we place the script, we still need to wait for the content to be loaded.

There is rare cases that the script needs to be executed before the browser loads the content. We put these types of script in the head tag. For example, the following MixPanel instructs web designers to put the script before the closing of the head tag.

mixpanel screenshot

You can find more on this in Yahoo’s developer guide.

I teach web design. I write books on HTML5 games development. I host classes on mobile web and app development. I made animation with Adobe Flash (now Animate).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.