简体   繁体   English

为什么 javascript 标签使用“文本”?

[英]Why do javascript tags use “text”?

Whenever we write a javascript,this is how we declare-每当我们编写 javascript 时,这就是我们声明的方式-

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

But i don't understand why it is declared as a text .但我不明白为什么它被声明为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因为它是纯文本而不是图像/或应用程序/但是它可能必须是应用程序,但没有人使用它,IE 会失败

Because text is what it used to be and some browsers will fail if we change it, you should keep text/javascript.因为 text 和以前一样,如果我们更改它,某些浏览器会失败,所以您应该保留 text/javascript。 Now since HTML5 it is dropped and since the default IS javascript, a lot of SO people advocate simply dropping it.现在因为 HTML5 它被丢弃了,因为默认是 javascript,很多 SO 人主张简单地丢弃它。 This might give validation errors in some DOCTYPES这可能会在某些 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这也是不使用 onclick=" javascript: something()" 的一个很好的理由,除非您使用一些 IDE 并希望快速找到您的内联处理程序

What is the javascript MIME type for the type attribute of a script tag? 脚本标签的 type 属性的 javascript MIME 类型是什么?

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").脚本语言被指定为内容类型(例如,“text/javascript”)。 Authors must supply a value for this attribute.作者必须为此属性提供一个值。 There is no default value for this attribute.此属性没有默认值。

Reference: w3参考: w3

Because it is.因为它是。 Javascript source is text. Javascript 来源是文本。 This predicate will determine how the data is transported.该谓词将确定数据的传输方式。 Then, the type of text (Javascript source) determines how it is used.然后,文本的类型(Javascript 源代码)决定了它的使用方式。

For legacy reasons.出于遗留原因。 Technically , we're supposed to use application/javascript or application/ecmascript , but nobody really does.从技术上讲,我们应该使用application/javascriptapplication/ecmascript ,但没有人真正这样做。

The type attribute identifies the content between the <script> and </script> tags. type 属性标识<script></script>标记之间的内容。

The MIME type consists of two parts: one media type and one subtype. MIME 类型由两部分组成:一种媒体类型和一种子类型。 For JavaScript, the MIME type is "text/javascript".对于 JavaScript,MIME 类型为“text/javascript”。

Although as Douglas Crockford says leave it out .尽管正如 Douglas Crockford 所说的那样,将其排除在外

The text type was probably chosen without any specific reason: As JavaScript is textual, they chose text .可能没有任何具体原因选择了文本类型:由于 JavaScript 是文本,他们选择了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 ).但是已知对此类内容使用文本类型是有问题的,因为换行符必须表示为 CRLF,反之亦然,并且假定charset参数的默认值是 US-ASCII (此外, HTTP 指定了 ISO 8859-1 作为默认字符集)。

That's why they introduced application/javascript as the MIME type for JavaScript .这就是他们引入application/javascript作为 JavaScript 的 MIME 类型的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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