By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Why people use this style of javascript declaration?

P: n/a
e.g.

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

but not

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

for any reason?

Sep 10 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a


ho******@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.

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/
Sep 10 '06 #2

P: n/a
Martin Honnen wrote on 10 sep 2006 in comp.lang.javascript:
>

ho******@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)
Sep 10 '06 #3

P: n/a
ho******@gmail.com wrote:
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></scriptelement 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

Sep 10 '06 #4

P: n/a


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/#guidelinesabout
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</pand not <p />)"
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 10 '06 #5

P: n/a
VK
ho******@gmail.com wrote:
<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 :-)

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

Sep 10 '06 #6

P: n/a
ho******@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.
Sep 10 '06 #7

P: n/a
ho******@gmail.com wrote:
<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.

--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 10 '06 #8

P: n/a

Martin Honnen 寫道:
ho******@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.

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

Sep 11 '06 #9

P: n/a


ho******@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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 11 '06 #10

P: n/a
VK
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>...</scripttags.
Respectively <scripttag 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.

Sep 11 '06 #11

P: n/a
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/
Sep 12 '06 #12

P: n/a
VK

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?

Sep 12 '06 #13

P: n/a
ho******@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?
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

Sep 24 '06 #14

P: n/a
pe**********@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
<scriptelements 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.
--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 24 '06 #15

P: n/a
pe**********@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.

Sep 24 '06 #16

P: n/a
In article <ef*******************@news.demon.co.uk>, Richard Cornford
<Ri*****@litotes.demon.co.ukwrites

<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
Sep 27 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.