473,748 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2157
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.cwrot e:
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#ed ef-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.javas cript message <11************ ****@proxy01.ne ws.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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 20 '07 #5
In comp.lang.javas cript message <47************ **@PointedEars. de>, Mon,
22 Oct 2007 08:44:32, Thomas 'PointedEars' Lahn <Po*********@we b.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
possibilitie s are there, generally?"
Then you should learn to understand English, because that is exactly
what he asked for.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demo n.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.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.javas cript message <47************ **@PointedEars. de>, Tue,
23 Oct 2007 18:57:29, Thomas 'PointedEars' Lahn <Po*********@we b.de>
posted:
>Dr J R Stockton wrote:
>In comp.lang.javas cript message <47************ **@PointedEars. de>, Mon,
22 Oct 2007 08:44:32, Thomas 'PointedEars' Lahn <Po*********@we b.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
possibiliti es 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
possibilitie s (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.demo n.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.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.dem on.co.ukwrote in message
news:mz******** ******@invalid. uk.co.demon.mer lyn.invalid...
In comp.lang.javas cript message <47************ **@PointedEars. de>, Tue,
23 Oct 2007 18:57:29, Thomas 'PointedEars' Lahn <Po*********@we b.de>
posted:
>>Dr J R Stockton wrote:
>>In comp.lang.javas cript message <47************ **@PointedEars. de>, Mon,
22 Oct 2007 08:44:32, Thomas 'PointedEars' Lahn <Po*********@we b.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
possibilitie s 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
possibiliti es (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.demo n.co.uk Turnpike v6.05
MIME.
Web <URL:http://www.merlyn.demo n.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.de mon.co.ukwrote in messagenews:mz* *************@i nvalid.uk.co.de mon.merlyn.inva lid...
>
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.de mon.co.ukwrote in
messagenews:mz **************@ invalid.uk.co.d emon.merlyn.inv alid...

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

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

Similar topics

5
1470
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 there (which int complete, but thats another post/story) which sorts the data A-Z or Z-A. The problem i am having is that the loop row (which isnt visible on the ASP page, as its ASP) is being sorted as well.
1
2953
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 content dynamically within the wonderful framework I created. Whilest I look into the various methods of delivering content within a container of one form or another in the existing HTML, I am also exploring the possibility of using some form of...
2
6907
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> into htm (or html) file fails. How can I successfully config Apache2 to work with embedded PHP and javascript at same time? TIA,
9
6142
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 for easy editing. I'm hoping there is some sort of include or import directive that I could use. Or if no such directive exists, I'm wondering if anyone has written one which I could use. I need to do this without any server side scripting....
11
2784
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 < or several others, it converts them to html, such as &amp; or &lt; which can sometimes cause my scripts not to work. How can I prevent ASP.NET from doing this? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
17
2733
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 src="/shared/scripts/common.js?revision=1.6" type="text/javascript">. I am trying to see if there is any big deal to this or a best practice that is starting to creep up in the JavaScript community. If this is used only as a way to distinguish what...
2
10917
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 script block, I call the python function and store the return value in a variable. I need to be able to display this returned value on the webpage that the xsl generates. I tried to do a document.write() but found that document.write() causes...
3
9863
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 it answered my question I am just to stupid to realize it I would like to apologize in advance. This is my very first attempt at using Javascript so any help at all is greatly appreciated. I read the W3Schools Tutorial on Javascript so that and some...
9
3026
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 variable. This all works fine. However, I want it to loop within a javascript function every 5 seconds. The function does other stuff, so looping within this function is essential (I've simplified it in the example below). Any advice would be...
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.