1

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.

1

6 Answers 6

3

Because it is. Javascript source is text. This predicate will determine how the data is transported. Then, the type of text (Javascript source) determines how it is used.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

This doesn’t answer why it’s the „text“ type.
3

For legacy reasons. Technically, we're supposed to use application/javascript or application/ecmascript, but nobody really does.

Comments

3

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

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.