The Type and
Language attributes of
JavaScript
In this tutorial I'll explain the two main attributes of
JavaScript that go inside the <script> tag, and their purpose.
The type attribute
In the tutorial JavaScript primer, I briefly mentioned the
"type" attribute of
JavaScript, and how it is used mainly so your JavaScript
validates as part of a well form
document. This is the reason why most JavaScripts written these days at
least carry this attribute:
<script type="text/javascript">
//script here
</script>
Depending on the
doctype
you're using for your page, such as XHTML strict, adding the "type"
attribute alone may still not cause the script to validate. In this case you
can take it a step further, by declaring all contents of your script as a
CDATA:
<script type="text/javascript">
<!-- <![CDATA[
//script here
// ]]> -->
</script>
Don't worry, this doesn't break the script in any way, as
it's just JavaScript comments with additional code thrown in, and your
JavaScript is sure to validate as a result.
The language attribute of JavaScript
The basic form of the language attribute is very easy to
implement. Simply shove in the keyword "language=", followed by
"JavaScript", into your <script> tag:
<script language="JavaScript">
alert("hi!")
</script>
This explicitly declares to the browser that your script is
a JavaScript (instead of VBscript, for example). The "language" attribute
while not essential to declare, is useful in certain cases. Lets talk about
that next.
|