By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Placement and use of Scripts

The Placement and use of Scripts, can greatly effect performance to the worse or the better. This Tutorial is about Performance issues releted to Scripts.

Edited: 2012-10-11 08:46

Limiting the use of Scripts, can both work to decrease performance, and increase it, all depending on what the script is used for. Some scripts are specifically made to increase performance, or avoid unnecessary server-side processing.

Keeping scripts in the body section, preferably as close to the closing body tag as possible, will grant some performance increase.

If your pages don't depend on the scripts, its best to keep them in the body section. This is because whatever is placed in the head, needs to be executed before the page can be rendered.

Limiting the use of Scripts

Some scripts are entirely unnecessary, often used to overcome simple browser specific issues, which are solved in newer browser versions.

You should also avoid making advanced calculations, just to force a certain layout, which may have been hard or not otherwise possible using HTML and CSS alone.

Using scripts to increase performance

You can use scripts to validate user input, before its submitted to the server. This would free up some otherwise wasted server-side processing, you would however still need to validate things on the server as well, to avoid potential security hazards.

Using Ajax to only update the part of the website which needs to be updated, may lower your bandwidth usage, and limit wasted HTTP requests. But be sure to provide an alternative, so that search engines can easily index your content.

Advertisements

Ads on a page can slow it down considerably, so consider limiting the use of ads, to a few carefully selected spots on your pages.

Other website scripts

Some statistic modules, like Google analytics, has been known to slow down sites to a complete halt, only to show a blank page. This is because you need to place it further down in your HTML, preferably as close to the closing body tag as possible.

When you place your scripts this far down, you do at least ensure that the content before, will be rendered, regardless whether the script is executed properly.