Re: Is there a correct place to put javascript within a html file?



On 20 Oct, 02:00, RobG <rg...@xxxxxxxxxxxx> wrote:
On Oct 20, 2:25 am, mark4asp <mark4...@xxxxxxxxx> wrote:

I ask this

You should not assume that we can see or remember the subject of your
post when reading the body.

because only today I read advice telling me to put it
within the <head> element.

Reasonable advice because that is one place that script elements are
allowed, they are also permitted in the body.

This is the same advice I've always been
given but no one ever explained why.

The HTML specification tells you where script elements are permitted:

<URL:http://www.w3.org/TR/html4/interact/scripts.html#edef-SCRIPT>



Last week I was told to put it at the bottom of the html just before </
body>; the reason being that html won't start rendering until all the
script files have downloaded and if they're all in the head ... Right
now I'm putting all my javascript at the end of the html file seems
like the sensible thing to do; at least there's a reason for it.

You haven't provided any reason at all. Where you place script
elements depends on their intended purpose. There are two guiding
principles:

1. Code should be loaded before there is any chance it might be
called, and

2. If code requires access to DOM elements, it should not be called
before they might exist and should not error if it is.

How you achieve the above is up to you, but the general way is to
achieve the first is to load scripts containing general functions in
the head.

There are a variety of ways to achieve the second - inserting
functions intended to be run when the DOM is ready rather than onload
into a script element just before the closing body tag is one. There
are others, such as using a general "DOM ready" function or waiting
for the load event to occur.

--
Rob

Thanks Rob. In future, I will generally place my script just before
the </body> tag - unless there is a reason not to. Even then it will
be a rare bit of code that is needed in the head element. If all my
javascript is positioned just before the </body> then the various html
elements must be rendered or in the process of being rendered before
and javascript can run. I don't have much javascript executing on
window.onload but I suppose I could put code in there to check for the
existence of an object before I use it, set and interval and recurse
back?

I'm trying to follow Steven Sounders advice from "High Performance Web
Sites"


.



Relevant Pages

  • Re: No scroll bar for page + page error(s)
    ... that the javascript in the head preloads ... An error has occurred in the script on this page ... >>FrontPage Resources, WebCircle, MS KB Quick Links, etc. ...
    (microsoft.public.frontpage.client)
  • Javascript file not being called at appropriate time in 2.0?
    ... I have this script in my tag: ... and paste the actual script (the javascript code, ... exact same place in the <HEAD> tag, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: image popup window title
    ... head and I am left still just as perplexed as before. ... <script language='javascript'> ... Only if PopupPic uses the second parameter as the title of the new ... Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ ...
    (comp.lang.javascript)
  • Re: Bash background process/wait script problem
    ... what is the reason for this variable? ... script to avoid manipulating the variable within the subshell .. ... it in your head. ... Mark Taylor ...
    (comp.unix.aix)
  • Re: Posting form contents without having to push the button
    ... <script type="text.javascript" src="http://www.whereever.com/ ... stick it in the head and the data is sent to the ... should return the correct content-type for javascript. ... JFTR: There are registered MIME types for JavaScript: ...
    (comp.lang.php)