Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > More than a single script block within a single HEAD and BODY

Reply
Thread Tools

More than a single script block within a single HEAD and BODY

 
 
Water Cooler v2
Guest
Posts: n/a
 
      04-18-2006
Questions:

1. Can there be more than a single script block in a given HEAD tag?
2. Can there be more than a single script block in a given BODY tag?

To test, I tried the following code. None of the script gets executed.
Can someone please give me a direction as to what I may be missing?

Thanks.





<!--The purpose of this program will be to:

1. Use an external script;
2. To use more than one SCRIPT tag inside a HEAD tag; and
3. To use more than one SCRIPT tag inside a BODY tag.
-->

<HTML>

<HEAD>

<SCRIPT language="JavaScript type="text/javascript">
<!--
document.write("This is the first of the script tags within the HEAD
tag.");
-->
</SCRIPT>

<SCRIPT language="JavaScript type="text/javascript">
<!--
document.write("This is the second script tag within the HEAD
tag.");
-->
</SCRIPT>

<SCRIPT language="JavaScript type="text/javascript"
src="TheExternalScript1.js"></SCRIPT>
</HEAD>



<BODY>
<SCRIPT language="JavaScript type="text/javascript">
<!--
document.write("\
The purpose of this program will be to:<BR/><BR/>\
\
1. Use an external script;<BR/>\
2. To use more than one SCRIPT tag inside a HEAD tag; and<BR/>\
3. To use more than one SCRIPT tag inside a BODY tag.<BR/>");
//-->
</SCRIPT>

<P>Some intermittent text.</P>

<SCRIPT language="JavaScript type="text/javascript">
<!--
document.write("This is the second of the script tags within the
BODY tag.");
-->
</SCRIPT>

<P>More HTML text.</P>

<SCRIPT language="JavaScript type="text/javascript">
<!--
document.write("This is the third of the script tags within the BODY
tag.");
-->
</SCRIPT>

<P>More and more HTML.</P>

<SCRIPT language="JavaScript type="text/javascript"
src="TheExternalScript2.js"></SCRIPT>

<P>Finally, the concluding part of the HTML text.</P>

</BODY>

</HTML>

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-18-2006
Water Cooler v2 wrote:
> Questions:
>
> 1. Can there be more than a single script block in a given HEAD tag?


Inside a script *element*, yes. You can have as many as you like.


> 2. Can there be more than a single script block in a given BODY tag?


Inside a script *element*, yes. You can have as many as you like.


> To test, I tried the following code. None of the script gets executed.


Because there are errors in the and HTML - *always* start with valid
HTML. The W3C HTML validator lets you paste markup directly into the
validation form.

<URL:http://validator.w3.org/>


> Can someone please give me a direction as to what I may be missing?


See below.


> <!--The purpose of this program will be to:
>
> 1. Use an external script;
> 2. To use more than one SCRIPT tag inside a HEAD tag; and
> 3. To use more than one SCRIPT tag inside a BODY tag.
> -->
>
> <HTML>
>
> <HEAD>
>
> <SCRIPT language="JavaScript type="text/javascript">


The language attribute is deprecated, remove it. Keep type.


> <!--


Don't use HTML comments inside script elements, they are useless.


> document.write("This is the first of the script tags within the HEAD
> tag.");


The result of the document.write will be placed immediately after the
script element. It is still inside the head, the browser isn't supposed
to display any content in the head so the browser must decide whether to
employ error correction and end the head and display the text, or
continue the head to the closing tag and not show the text.

Whatever choice is made may be inconsistent across different browsers.


> -->


This is a meaningless script statement. To use HTML comments at all,
the closing tag should be quoted:

// -->

But just don't use them.


> </SCRIPT>
>
> <SCRIPT language="JavaScript type="text/javascript">
> <!--
> document.write("This is the second script tag within the HEAD
> tag.");
> -->
> </SCRIPT>


Comments as above.


>
> <SCRIPT language="JavaScript type="text/javascript"
> src="TheExternalScript1.js"></SCRIPT>


What is in the external file? You should have a title element inside
the head. Putting one right at the top might encourage error correction
to move the closing head tag up higher and move the body up too -
causing the text written by document.write to be displayed (or not).

> </HEAD>
>
>
>
> <BODY>
> <SCRIPT language="JavaScript type="text/javascript">


Here you are missing the closing quote after language="JavaScript

That will cause a variety of errors, essentially reversing the sense of
following double quotes.

> <!--
> document.write("\


It is legal to quote new lines, but much preferred to use concatenation:

document.write("The purpose ..."
+ "<br>1. Use..."
+ "<br>2. To..."
);

> The purpose of this program will be to:<BR/><BR/>\


Don't use XHTML style empty element tags in what is HTML (despite the
missing, required DOCTYPE element).

Forward slashes "/" within document.write strings should be quoted
because they should indicate the close of the script element, most
browsers tolerate them regardless.

> \
> 1. Use an external script;<BR/>\
> 2. To use more than one SCRIPT tag inside a HEAD tag; and<BR/>\
> 3. To use more than one SCRIPT tag inside a BODY tag.<BR/>");


Fix those errors and apply to the rest of the page.

[...]


--
Rob
 
Reply With Quote
 
 
 
 
Water Cooler v2
Guest
Posts: n/a
 
      04-18-2006
Many thanks, Rob. Mike, on another thread (two threads below) had
advised me with the same invaluable tips as you've given. Thank you
very much. I will incorporate them in any code I write from now on. I
wrote this snippet before I read the two replies (yours and Mike's).

I am sorry I forgot to mention that I had two external script files
with the same names as the references above (TheExternalScrip1.js and
TheExternalScript2.js) so there wasn't any problem with that. In the
external files, I had just written a document.write statement each.

I spotted the cause of the bug. I had not terminated the string
"JavaScript" in any of the declarations of the opening SCRIPT tag, like
so:

<SCRIPT language="JavaScript type="text/javascript">

In any case, I'll omit the language attribute and heed to the other
advise you've mentioned as well. Thanks again.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
Issue in executing more than one perl script from Single perl script nilesh.sonawane@gmail.com Perl Misc 3 11-01-2007 06:19 PM
moving script from body to head toby989@hotpop.com Javascript 4 09-29-2006 04:37 AM
Adding script block to <head> Steve C. Orr [MVP, MCSD] ASP .Net 3 03-26-2005 03:44 AM
PHP and ASP.NET go HEAD to HEAD showme ASP .Net 13 07-10-2004 09:44 PM



Advertisments