Jump to content

Placing Javascript At The Bottom Of The Page


djp1988
 Share

Recommended Posts

It is recommended to place javascript at the bottom of the html page when possible to speed up the page, but is this respecting the web standards by having a <script> tag importing external javascript placed at the bottom of a page ?

Link to comment
Share on other sites

Web standards still let you put a script tag anywhere you want it. Two reasons:1. Using document.write(), a scriptlet can generate HTML in the exact spot where it is supposed to be. You might want this for a calendar or clock feature, an ad, an image du jour, etc.2. If your scriptlet runs inline (ie., as it's being loaded, not in response to an event) and it needs to refer to items in the page, then it can be placed below those items, ensuring that the items are already part of the DOM when they are needed.Either of these situations can be replaced by a window.onload handler, but there is nothing inherently wrong with them.If your page is significantly huge (over 5K) then running scripts in this way can enhance the speed. On a small page, you might not notice.Or maybe the question is about how big the script is, and how that effects the rest of the page? If the script doesn't need to run right away, and the script is big (a true "webapp") then putting it last will cause other page elements to load faster, yes. (Something tells me this is what you really want to know, and I didn't catch on till now.)

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...