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:
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>
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!