Home > Web Front-end > CSS Tutorial > Is the \'type\' Attribute in JavaScript and CSS Scripts Really Necessary?

Is the \'type\' Attribute in JavaScript and CSS Scripts Really Necessary?

Patricia Arquette
Release: 2024-11-25 16:18:13
Original
215 people have browsed it

Is the 'type' Attribute in JavaScript and CSS Scripts Really Necessary?

Why Specify the 'type' Attribute in JavaScript or CSS Scripts?

Although web servers determine MIME types, the question arises: why do we include the 'type="text/javascript"' or 'type="text/css"' attribute in HTML scripts? Isn't this attribute redundant and ignored?

Answer

According to Douglas Crockford:

  • The 'type="text/javascript"' attribute is optional. Since Netscape 2, JavaScript has been the default programming language in browsers. In XHTML, this attribute is required but unnecessary, while in HTML, it's best to omit it.
  • W3C introduced a 'type' attribute that accepts a MIME type but did not standardize it, leading to variations like "text/javascript" and "application/ecmascript." However, browsers will always default to JavaScript as the programming language.

For demonstration purposes, five scripts with different 'type' attributes were tested in Chrome and IE8:

<script type="application/ecmascript">alert("1");</script>
<script type="text/javascript">alert("2");</script>
<script type="baloney">alert("3");</script>
<script type="">alert("4");</script>
<script>alert("5");</script>
Copy after login

In Chrome, scripts 1-4 worked, while script 3 (type="baloney") failed. In IE8, scripts 1 and 3 failed to execute.

Based on this small sample, it appears that the 'type' attribute can be safely ignored. However, if used, it should conform to legal values supported by the browser.

The above is the detailed content of Is the \'type\' Attribute in JavaScript and CSS Scripts Really Necessary?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template