473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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

I ask this because only today I read advice telling me to put it
within the <headelement. This is the same advice I've always been
given but no one ever explained why.

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.

Oct 19 '07 #1
10 2124
On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:
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 <headelement.
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/...ml#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
Oct 20 '07 #2
RobG wrote:
On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:
>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 <headelement.

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/...ml#edef-SCRIPT >
Well I read it and it doesn't. It says in the head or the body. What
else is there?
Oct 20 '07 #3
On Oct 20, 11:37 am, The Natural Philosopher <a...@b.cwrote:
RobG wrote:
On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:
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 <headelement.
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>

Well I read it and it doesn't.
Yes, it does.

It says in the head or the body.
See?

What else is there?
Life, the universe and everything. :-)
--
Rob

Oct 20 '07 #4
In comp.lang.javascript message <11****************@proxy01.news.clara.n
et>, Sat, 20 Oct 2007 02:37:11, The Natural Philosopher <a@b.cposted:
>
Well I read it and it doesn't. It says in the head or the body. What
else is there?
Before the head; between head and body; after the body; inside or
outside the HTML. Before DOCTYPE.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 20 '07 #5
In comp.lang.javascript message <47**************@PointedEars.de>, Mon,
22 Oct 2007 08:44:32, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>Dr J R Stockton wrote:
>[...] The Natural Philosopher <a@b.cposted:
>>Well I read it and it doesn't. It says in the head or the body. What
else is there?

Before the head; between head and body; after the body; inside or
outside the HTML. Before DOCTYPE.
>But I don't think the question of the OP was meant as "What other
possibilities are there, generally?"
Then you should learn to understand English, because that is exactly
what he asked for.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Oct 22 '07 #6
In comp.lang.javascript message <47**************@PointedEars.de>, Tue,
23 Oct 2007 18:57:29, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>Dr J R Stockton wrote:
>In comp.lang.javascript message <47**************@PointedEars.de>, Mon,
22 Oct 2007 08:44:32, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>>Dr J R Stockton wrote:
[...] The Natural Philosopher <a@b.cposted:
Well I read it and it doesn't. It says in the head or the body. What
else is there?
Before the head; between head and body; after the body; inside or
outside the HTML. Before DOCTYPE.
>>But I don't think the question of the OP was meant as "What other
possibilities are there, generally?"

Then you should learn to understand English, because that is exactly
what he asked for.

It is not a matter of understanding English or not, but of the capability of
applying common sense which you either lack or have deliberately not used here.

"What else is there?" in this context clearly means "What other valid
possibilities (other than in the `head' or the `body' [element]) are there
to place a HTML `script' element?", and the only good and correct answer
would have been "None."
You, too, should learn to understand English, complete with nuances,
correctly. It's a non-trivial intellectual task, beyond the
capabilities of most non-British.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Oct 23 '07 #7

"Dr J R Stockton" <jr*@merlyn.demon.co.ukwrote in message
news:mz**************@invalid.uk.co.demon.merlyn.i nvalid...
In comp.lang.javascript message <47**************@PointedEars.de>, Tue,
23 Oct 2007 18:57:29, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>>Dr J R Stockton wrote:
>>In comp.lang.javascript message <47**************@PointedEars.de>, Mon,
22 Oct 2007 08:44:32, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
Dr J R Stockton wrote:
[...] The Natural Philosopher <a@b.cposted:
>Well I read it and it doesn't. It says in the head or the body. What
>else is there?
Before the head; between head and body; after the body; inside or
outside the HTML. Before DOCTYPE.

But I don't think the question of the OP was meant as "What other
possibilities are there, generally?"

Then you should learn to understand English, because that is exactly
what he asked for.

It is not a matter of understanding English or not, but of the capability
of
applying common sense which you either lack or have deliberately not used
here.

"What else is there?" in this context clearly means "What other valid
possibilities (other than in the `head' or the `body' [element]) are there
to place a HTML `script' element?", and the only good and correct answer
would have been "None."

You, too, should learn to understand English, complete with nuances,
correctly. It's a non-trivial intellectual task, beyond the
capabilities of most non-British.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05
MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, &
links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
It is beyond the capabilities of most, if not all British, too, English is
too ambiguous.

The classic hand-grenade instructions:
"Pull out the pin and throw it."
Oct 24 '07 #8
On Oct 24, 6:40 am, "David Cox" <nos...@nospam.comwrote:
"Dr J R Stockton" <j...@merlyn.demon.co.ukwrote in messagenews:mz**************@invalid.uk.co.demon.m erlyn.invalid...
>
It is beyond the capabilities of most, if not all British, too, English is
too ambiguous.
English is not ambiguous. Its beauty is the difference in nuance
between different words with very similar meanings. That is why it is
such a rich language for good literature.

www.richardfisher.com
Oct 24 '07 #9
On Wed, 24 Oct 2007 04:57:30 -0700, Helpful person wrote:
On Oct 24, 6:40 am, "David Cox" <nos...@nospam.comwrote:
>"Dr J R Stockton" <j...@merlyn.demon.co.ukwrote in
messagenews:mz**************@invalid.uk.co.demon. merlyn.invalid...

It is beyond the capabilities of most, if not all British, too, English
is too ambiguous.
English is not ambiguous. Its beauty is the difference in nuance
between different words with very similar meanings. That is why it is
such a rich language for good literature.
The irony is that Thomas is at the top of the list of those who would
point out the minutia of phrasing in a post and drive the subject into the
ground.

--
I told you this was going to happen.

Oct 24 '07 #10
On 20 Oct, 02:00, RobG <rg...@iinet.net.auwrote:
On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:
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 <headelement.

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 </bodytag - 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 </bodythen 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"
Oct 25 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fawke101 | last post by:
Hi There, I have a table with 3 rows. 1st has the table/column headers, 2nd contains the column data and the 3rd is where the loop for the SQL statement lies. I also have a JS sort function on...
1
by: Catherine Lynn Smith | last post by:
OK, I am looking for advice or tips and suggestions. I have overcome a lot of the obstructions I had to building the page design my customer was asking for. I am looking now at ways to deliver...
2
by: Bernhard Georg Enders | last post by:
After issuing the command (apache httpd.conf file) AddType application/x-httpd-php .php .htm .html any attempt to include javascript code <script src="file.js" type="text/javascript"></script>...
9
by: Trenqo 0 | last post by:
I'm looking for a way to include javascript files from within a ".js" file. This would allow me to only need to link to one ".js" file, and yet still organize my functions into non gargantuan files...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
17
by: NeoAlchemy | last post by:
I am starting to find more web pages that are using a query parameters after the JavaScript file. Example can be found at www.opensourcefood.com. Within the source you'll see: <script...
2
by: neeebs | last post by:
Hi, I'm not sure if this is a javascript problem per se, but here goes. I have an xsl document with a python function defined within a <script> block. Elsewhere in the xsl file, within a python...
3
by: zac540 | last post by:
Hey everyone! First of all I'd like to say that I did my best to look for any other relevant posts. The best I found was this interesting thread.. http://bytes.com/forum/thread594982.html If...
9
by: mrcheeky | last post by:
Hi, I'm stuck, but it's almost working! From a html page, my javascript calls a server-side php script. The php reads a value from a server-side .txt file and passes it back as a javascript...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.