Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why people use this style of javascript declaration?

1 view
Skip to first unread message

howa...@gmail.com

unread,
Sep 10, 2006, 11:47:02 AM9/10/06
to
e.g.

<script type="text/javascript" src="js/common.js"></script>

but not

<script type="text/javascript" src="js/common.js" />

for any reason?

Martin Honnen

unread,
Sep 10, 2006, 11:55:31 AM9/10/06
to

howa...@gmail.com wrote:

Most people write HTML 4 and there


<script type="text/javascript" src="js/common.js"></script>

is the proper way.

Some people write XHTML 1 and serve it as text/html and then


<script type="text/javascript" src="js/common.js"></script>

is the proper way too.

If you write XHTML and serve with an XML or XHTML MIME type like
application/xml or text/xml or application/xhtml+xml then you can choose
between the two ways you have shown as then your markup is parsed with
an XML parser and in terms of XML there is no difference between the two
forms, both are allowed.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Evertjan.

unread,
Sep 10, 2006, 11:57:50 AM9/10/06
to
Martin Honnen wrote on 10 sep 2006 in comp.lang.javascript:

>
>
> howa...@gmail.com wrote:
>
>> e.g.
>>
>> <script type="text/javascript" src="js/common.js"></script>
>>
>> but not
>>
>> <script type="text/javascript" src="js/common.js" />
>>
>> for any reason?
>
> Most people write HTML 4 and there
> <script type="text/javascript" src="js/common.js"></script>
> is the proper way.
>
> Some people write XHTML 1 and serve it as text/html and then
> <script type="text/javascript" src="js/common.js"></script>
> is the proper way too.

Eh?

> If you write XHTML and serve with an XML or XHTML MIME type like
> application/xml or text/xml or application/xhtml+xml then you can choose
> between the two ways you have shown as then your markup is parsed with
> an XML parser and in terms of XML there is no difference between the two
> forms, both are allowed.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

peterm...@gmail.com

unread,
Sep 10, 2006, 12:00:36 PM9/10/06
to
howa...@gmail.com wrote:
> Re: Why people use this style of javascript declaration?

I haven't looked before but this email prompted me to scan through the
doctype definition

<URL: http://www.w3.org/TR/html4/strict.dtd>

Here are a couple relevant parts I gleaned. The <br /> element id
defined as empty and so are all the others you see regularly without a
closing tag like <img /> and <meta />. The <script></script> element is
not defined as empty.

<!ELEMENT BR - O EMPTY -- forced line break -->
<!ATTLIST BR
%coreattrs; -- id, class, style, title --
>

<!ELEMENT SCRIPT - - %Script; -- script statements -->
<!ATTLIST SCRIPT
charset %Charset; #IMPLIED -- char encoding of linked
resource --
type %ContentType; #REQUIRED -- content type of script
language --
src %URI; #IMPLIED -- URI for an external script --
defer (defer) #IMPLIED -- UA may defer execution of
script --
event CDATA #IMPLIED -- reserved for possible future
use --
for %URI; #IMPLIED -- reserved for possible future
use --
>

-Peter

Martin Honnen

unread,
Sep 10, 2006, 12:03:03 PM9/10/06
to

Evertjan. wrote:

> Martin Honnen wrote on 10 sep 2006 in comp.lang.javascript:

>>Most people write HTML 4 and there
>> <script type="text/javascript" src="js/common.js"></script>
>>is the proper way.
>>
>>Some people write XHTML 1 and serve it as text/html and then
>> <script type="text/javascript" src="js/common.js"></script>
>>is the proper way too.
>
>
> Eh?

HTML 4 defines its script element here:
<http://www.w3.org/TR/html4/interact/scripts.html#edef-SCRIPT>
clearly saying "Start tag: required, End tag: required".

XHTML 1 has this section <http://www.w3.org/TR/xhtml1/#guidelines> about
serving XHTML 1 as text/html and section C3 there says:
"Given an empty instance of an element whose content model is not
EMPTY (for example, an empty title or paragraph) do not use the
minimized form (e.g. use <p> </p> and not <p />)"

VK

unread,
Sep 10, 2006, 12:03:18 PM9/10/06
to

Did you try to use it in a document served as text/html? This is the
answer: because it doesn't work :-)

<script> tag requires closing tag. The second option will work only for
a document parsed as XHTML (not HTML). Please note: *parsed* as XHTML
(thus served with the proper Content-Type), not just being called XHTML
in the document head section and/or conforming to XHTML syntax rules.

You may read
<http://groups.google.com/group/comp.infosystems.www.authoring.html/browse_frm/thread/6ff8329925aae8a6/ffe8f9f366be53ad>
if you have patience to follow that long and with numerous OT's
discussion.

Michael Winter

unread,
Sep 10, 2006, 12:07:42 PM9/10/06
to
howa...@gmail.com wrote:

> <script type="text/javascript" src="js/common.js"></script>
>
> but not
>
> <script type="text/javascript" src="js/common.js" />

The latter is an XML empty-element tag so it is not appropriate for use
in a HTML document[1].

Mike

Cross-posted, and follow-ups set, to comp.infosystems.www.authoring.html.


[1] I define a HTML document here to mean any document that is served
with a text/html MIME type, whether it looks like XHTML or not.

David Dorward

unread,
Sep 10, 2006, 12:09:31 PM9/10/06
to
howa...@gmail.com wrote:

The latter lacks the required end tag in HTML, is forbidden by Appendix C of
the XHTML 1.0 spec and breaks horribly in MSIE.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is

howa...@gmail.com

unread,
Sep 11, 2006, 7:18:26 AM9/11/06
to

Martin Honnen 寫道:

I know I should use, i.e.

<script type="text/javascript" src="js/common.js"></script>

for HTML or XHTML

but since there is no content inside the tag anyway, what are the
reason behind this style?

thanks.

Martin Honnen

unread,
Sep 11, 2006, 8:51:07 AM9/11/06
to

howa...@gmail.com wrote:


> I know I should use, i.e.
>
> <script type="text/javascript" src="js/common.js"></script>
>
> for HTML or XHTML
>
> but since there is no content inside the tag anyway, what are the
> reason behind this style?

The reasons where already given in the answers you got, HTML 4 as an
SGML application defines its syntax rules and requires that the script
element has a start tag and an end tag. XHTML as an XML application has
different rules. If you want to question the reasons of those who
defined those rules then you might want to try your luck in
comp.infosystems.www.authoring.html.

VK

unread,
Sep 11, 2006, 9:46:13 AM9/11/06
to

> > but since there is no content inside the tag anyway, what are the
> > reason behind this style?
>
> If you want to question the reasons of those who
> defined those rules then you might want to try your luck in
> comp.infosystems.www.authoring.html.

I say this question more relevant to JavaScript rather then to the HTML
tags as such.

The reason is historical (as many things in HTML/scripting).

Until versions NN3/IE3 the only browser with JavaScript support was
Netscape 2.x, and it did not support external .js files, only scripts
directly inserted into page between <script>...</script> tags.
Respectively <script> tag was added to HTML specs as the one requiring
closing tag.
With versions NN3/IE3 it became possible to use external .js files, but
the paired tag was used as the fall-back option for NN2 users. If both
src attribute and inline content were provided, NN3/IE3 users would get
the external .js file executed and inline content ignored. NN2 users
would get src ignored and inline block executed:

<script language="JavaScript" src="external.js">
window.alert("Your browser doesn't support external scripts");
</script>

(btw it is another FAQ when people are trying to combine both external
and inline scripts in one tag and getting only the external script
executed. If you ever tried it and wondered why then here is the
answer).

By NN4/IE4 the above became a standard with a bunch of legacy scripts
around, so it was too late to change anything.

Hope it helps.

Randy Webb

unread,
Sep 11, 2006, 10:02:11 PM9/11/06
to
VK said the following on 9/11/2006 9:46 AM:

>>> but since there is no content inside the tag anyway, what are the
>>> reason behind this style?
>> If you want to question the reasons of those who
>> defined those rules then you might want to try your luck in
>> comp.infosystems.www.authoring.html.
>
> I say this question more relevant to JavaScript rather then to the HTML
> tags as such.

<sarcasm>
The rules of xHTML and HTML are more relevant to JS than to HTML? Wow,
your logic is awesome!
</sarcasm>

Not really, it's just more VK crap noise is all.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

VK

unread,
Sep 12, 2006, 4:24:29 AM9/12/06
to

Randy Webb wrote:
> <sarcasm>
> The rules of xHTML and HTML are more relevant to JS than to HTML? Wow,
> your logic is awesome!
> </sarcasm>

You don't need to try so hard to be a moron on a plain place, you'll
have much better opportunities in the future. The question of how to
insert/link JavaScript code on a web-page is definitely one more
relevant to c.l.j.

This mechanics and historical background are given in my post you
missed to quote properly. Is September some moronic fair here?

peterm...@gmail.com

unread,
Sep 24, 2006, 5:19:55 AM9/24/06
to

I've been hunting for the reason we don't do the comment trick to hide
javascript. I can't remember which browser needed this. I think it was
a very old browser.

<script type="text/javascript">
<!--
alert("hi");
-->
</script>

Thanks,
Peter

David Dorward

unread,
Sep 24, 2006, 5:47:41 AM9/24/06
to
peterm...@gmail.com wrote:

> I've been hunting for the reason we don't do the comment trick to hide
> javascript.

In XHTML? Because it comments the JavaScript out (since the content model of
<script> elements has changed).

In HTML? Because browsers which need it have much bigger problems (such as
supporting HTTP 1.1).

> I can't remember which browser needed this. I think it was
> a very old browser.

Netscape 2 era IIRC.

Richard Cornford

unread,
Sep 24, 2006, 11:10:52 AM9/24/06
to
peterm...@gmail.com wrote:
<snip>

> I've been hunting for the reason we don't do the comment
> trick to hide javascript.

Did you find any justification for using it that has had any validity in
the last half decade? Sometime you don't do something became there is no
reason for ding it, rather than that you cannot find a reason for not
ding it.

> I can't remember which browser needed this.

Browsers pre-dating and contemporary with Netscape 2 would have no idea
what a SCRIPT element was and so would tend to treat their contents as
text to be displayed. Browsers produced after that time would have no
problem knowing to ignore SCRIPT elements even if they could not handle
scripts themselves.

> I think it was a very old browser.

Currently version 4 era browsers are considered very old (if not
dinosaurs), and even the preceding generation did not need 'script
hiding'.

> <script type="text/javascript">
> <!--
> alert("hi");
> -->

Since that the content of an HTML script element is script source code
the <!-- character sequence cannot be interpreted as mark-up in that
context. To enable script hiding to work at all (that is, not to cause
syntax errors in browsers that understand scripts) the <!-- sequences is
treated as an alternative opening single line comment token (alternative
to //). This is not true of the -->, which could not be treated
specially as it is a valid sequence of javascript operators ( x-->y -
would be a 'is x greater than y' expression with post-decrement of x).
The 'script hiding' tended to get around that by hiding the --> sequence
in a javascript single line comment. I.E. writing //--> instead of
just -->. Error correction in IE (and probably some other browsers) will
let you get away with just --> in some contexts, but your formulation
above will certainly result in script syntax errors in some browsers.

Richard.

John G Harris

unread,
Sep 27, 2006, 4:46:45 AM9/27/06
to
In article <ef676f$p66$3$8302...@news.demon.co.uk>, Richard Cornford
<Ric...@litotes.demon.co.uk> writes

<snip>


>Browsers pre-dating and contemporary with Netscape 2 would have no idea
>what a SCRIPT element was and so would tend to treat their contents as
>text to be displayed. Browsers produced after that time would have no
>problem knowing to ignore SCRIPT elements even if they could not handle
>scripts themselves.

<snip>

My modern e-mail/news reader doesn't understand SCRIPT elements either,
but that's deliberate policy. It means that invitations to re-enter my
bank details look like a suspicious mess. Oh, what a shame! If I want to
view an HTML e-mail in all its glory I can ask for it to be displayed in
a proper browser.

So the rule nowadays is that displaying scripts as text is the reader's
choice; we shouldn't do anything to override that choice. This is a bit
stronger than saying there's no point hiding scripts any more.

John
--
John Harris

0 new messages