Forum Moderators: open

Message Too Old, No Replies

what's the proper way to declare a script in HTML

I see multiple ways to introduce a script in HTML - which is the accepted

         

Shadows Papa

12:45 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



Greetings,
Yeah, it's a simple one but even pros appreciate being able to not think and give simple answers to simple questions now and then.
I see a lot of different ways to declare the start of a script in HTML. I suspect it's because things changed over the years, thus the "mix".
I'd like to know, which is proper - why, and will it work in all browsers that support scripts?

A. <script type="text/javascript">

B. <script language="JavaScript">

C. <script language="JavaScript1.2">

I also see a lot of scripts no longer use the
<!--
and
// -->
is this ok, or do they really need to be there?

Next, what are the pros and cons of putting the script IN the HTML vs. calling the script via an "include" OR like this:
<script language="JavaScript1.2" src="animate.js">

When answering, please keep in mind I'm dealing with plain HTML, not with the "x" ;-)

Thanks!

Shadows Papa

Birdman

1:15 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Choice A is the proper way to call a script, although the others will work.

<!--
and
// --> Hides the script from old browsers, which would simply print the coding of the script without them.

If the script is to be used on more than one page it is better to call the script externally. Most browsers will cache the script on the first pageview. Saves some downloading time for the user and also helps push the real content of the page up higher in the document.

Regards,
Birdman

BlobFisk

1:15 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Option (A) is the standards compliant way!

The language attribute is depricated, and almost 90% of browsers now use Javascipt1.2. The version of JS only really comes into play with very new Object Oriented JS and very old browsers.

HTH

DrDoc

3:57 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As for <!-- comment --> use...

First of all, keep in mind that these are HTML comments. When we say that they were used as a trick to hide JavaScript from "old" browsers -- we're talking really old! You really don't have to use them anymore...

Now, to take it one step further... If you're designing XML or XHTML pages -- you shouldn't use HTML style comments in your CSS/JavaScript. Why? Because XML based rendering engines are allowed to silently remove the contents of comments. This would, of course, cause your JavaScript/CSS not to work...