Jump to content

Placing Javascript At The Bottom Of The Page


djp1988

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.)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...