Why people use this style of javascript declaration?

H

howachen

e.g.

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

but not

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

for any reason?
 
M

Martin Honnen

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.

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.
 
P

petermichaux

Re: Why people use this style of javascript declaration?

e.g.

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

but not

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

for any reason?

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
 
M

Martin Honnen

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


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 />)"
 
V

VK

<script type="text/javascript" src="js/common.js"></script>
but not
<script type="text/javascript" src="js/common.js" />
for any reason?

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..._frm/thread/6ff8329925aae8a6/ffe8f9f366be53ad>
if you have patience to follow that long and with numerous OT's
discussion.
 
D

David Dorward

<script type="text/javascript" src="js/common.js"></script>
but not
<script type="text/javascript" src="js/common.js" />
for any reason?

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.
 
H

howachen

Martin Honnen 寫é“:
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.

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.
 
M

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?

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.
 
V

VK

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.
 
R

Randy Webb

VK said the following on 9/11/2006 9:46 AM:
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.
 
V

VK

Randy said:
<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?
 
P

petermichaux

e.g.

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

but not

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

for any reason?

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
 
D

David Dorward

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.
 
R

Richard Cornford

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.
 
J

John G Harris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top