Whenever we write a javascript,this is how we declare-
<Script type="text/javascript">
. . .
. . .
. . . </script>
But i don't understand why it is declared as a text.
Whenever we write a javascript,this is how we declare-
<Script type="text/javascript">
. . .
. . .
. . . </script>
But i don't understand why it is declared as a text.
type = content-type:
This attribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.
Reference: w3
For legacy reasons. Technically, we're supposed to use application/javascript or application/ecmascript, but nobody really does.
Because it is plain text and not image/ or application/ However it MIGHT HAVE to be application, but no-one uses it and IE will fail on it
Because text is what it used to be and some browsers will fail if we change it, you should keep text/javascript. Now since HTML5 it is dropped and since the default IS javascript, a lot of SO people advocate simply dropping it. This might give validation errors in some DOCTYPES
<script>
alert("UNLESS this is IE and there is a VBScript as the first script on the page,"+
"this WILL be JavaScript in all known browsers");
</script>
which is also a good reason not to use onclick="javascript: something()" unless you use some IDE and want to find your inline handlers quickly
What is the javascript MIME type for the type attribute of a script tag?
Which links to Specifying the scripting language
The type attribute identifies the content between the
<script>and</script>tags.The MIME type consists of two parts: one media type and one subtype. For JavaScript, the MIME type is "text/javascript".
Although as Douglas Crockford says leave it out.
The text type was probably chosen without any specific reason: As JavaScript is textual, they chose text.
But the use of the text type for this kind of content is known to be problematic as line breaks must be represented as a CRLF and vice versa and the default value of the charset parameter is assumed to be US-ASCII (additionally, HTTP specified ISO 8859-1 as default charset).
That’s why they introduced application/javascript as the MIME type for JavaScript.