简体   繁体   English

<script> tag vs <script type = 'text/javascript'> tag

[英]<script> tag vs <script type = 'text/javascript'> tag

I was just wondering, what is the difference between 我只是想知道,两者之间有什么区别

<script>

and

<script type = 'text/javascript'>

Is it different for different webservers? 不同的Web服务器是否不同?

For example,(I know it's incorrect to provide a link from w3schools, but look) 例如,(我知道提供w3schools的链接是不正确的,但是请看)

http://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst http://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst

Using chrome, I visited w3schools and I realised that the <script> tag is all I need. 使用chrome,我参观了w3schools,我意识到<script>标签是我所需要的。

However, when I did an offline javascript test, i realised that i need the 但是,当我进行离线javascript测试时,我意识到我需要

<script type = 'text/javascript'>

tag. 标签。 Why is this so? 为什么会这样呢?

In HTML 4, the type attribute is required. 在HTML 4中,type属性是必需的。 In my experience, all browsers will default to text/javascript if it is absent, but that behaviour is not defined anywhere. 以我的经验,如果不存在,所有浏览器将默认使用text / javascript,但是该行为未在任何地方定义。 While you can in theory leave it out and assume it will be interpreted as JavaScript, it's invalid HTML, so why not add it. 从理论上讲,虽然您可以省略它并假定它将被解释为JavaScript,但它是无效的HTML,所以为什么不添加它。

In HTML 5, the type attribute is optional and defaults to text/javascript 在HTML 5中,type属性是可选的,默认为text / javascript

Use <script type="text/javascript"> or simply <script> (if omitted, the type is the same). 使用<script type="text/javascript">或仅使用<script> (如果省略,则类型相同)。 Do not use <script language="JavaScript"> ; 不要使用<script language="JavaScript"> ; the language attribute is deprecated 语言属性已弃用

Ref : 参考
http://social.msdn.microsoft.com/Forums/vstudio/en-US/65aaf5f3-09db-4f7e-a32d-d53e9720ad4c/script-languagejavascript-or-script-typetextjavascript-?forum=netfxjscript http://social.msdn.microsoft.com/Forums/vstudio/zh-CN/65aaf5f3-09db-4f7e-a32d-d53e9720ad4c/script-languagejavascript-or-script-typetextjavascript-?forum=netfxjscript
and
Difference between <script> tag with type and <script> without type? 带类型的<script>标记和不带类型的<script>标记之间的区别?

Do you need type attribute at all? 您是否完全需要type属性?

I am using HTML5- No 我正在使用HTML5-

I am not using HTML5 - Yes 我没有使用HTML5-是

<script> is HTML 5. <script>是HTML 5。

<script type='text/javascript'> is HTML 4.x (and XHTML 1.x). <script type='text/javascript'>是HTML 4.x(和XHTML 1.x)。

<script language="javascript"> is HTML 3.2. <script language="javascript">是HTML 3.2。

Is it different for different webservers? 不同的Web服务器是否不同?

No. 没有。

when I did an offline javascript test, i realised that i need the <script type = 'text/javascript'> tag. 当我进行离线javascript测试时,我意识到我需要<script type = 'text/javascript'>标签。

That isn't the case. 事实并非如此。 Something else must have been wrong with your test case. 您的测试用例肯定有其他问题。

Douglas Crockford says : 道格拉斯·克罗克福德说

type="text/javascript"

This attribute is optional. 此属性是可选的。 Since Netscape 2, the default programming language in all browsers has been JavaScript. 从Netscape 2开始,所有浏览器中的默认编程语言都是JavaScript。 In XHTML, this attribute is required and unnecessary. 在XHTML中,此属性是必需的,也是不必要的。 In HTML, it is better to leave it out. 在HTML中,最好将其省略。 The browser knows what to do. 浏览器知道该怎么做。

In HTML 4.01 and XHTML 1(.1), the type attribute for <script> elements is required . 在HTML 4.01和XHTML 1(.1)中, <script>元素的type属性是必需的

<!-- HTML4 and (x)HTML -->
<script type="text/javascript"></script>


<!-- HTML5 -->
<script></script>

type attribute identifies the scripting language of code embedded within a script element or referenced via the element's src attribute. type属性标识嵌入在script元素中或通过元素的src属性引用的代码的脚本语言。 This is specified as a MIME type; 这被指定为MIME类型。 examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. 受支持的MIME类型的示例包括text / javascript,text / ecmascript,application / javascript和application / ecmascript。 If this attribute is absent, the script is treated as JavaScript. 如果缺少此属性,则将该脚本视为JavaScript。

Ref: https://developer.mozilla.org/en/docs/Web/HTML/Element/script 参考: https : //developer.mozilla.org/en/docs/Web/HTML/Element/script

You only need <script></script> Tag that's it. 您只需要<script></script>标记即可。 <script type="text/javascript"></script> is not a valid HTML tag, so for best SEO practice use <script></script> <script type="text/javascript"></script>不是有效的HTML标记,因此,为了获得最佳SEO实践,请使用<script></script>

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

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