简体   繁体   中英

Why do javascript tags use “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 .

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

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 (eg, "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.

Reference: w3

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM