JavaScript and CSS Type
Sublime Text 的 Emmet 插件,在生成 script 和 style 标签时,都不带 type 声明:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <style></style> <script></script> </head> <body> </body> </html>
而 PHPStorm 在快捷生成标签的时候,却自带 type,但是 PHPStorm 过重。
为了搞清楚这个 type 声明是否必须,今天查了一些资料,这里记录下:
Style
Question
What does type="text/css" within the HTML code <style type="text/css"> mean?
What does it mean?
When I removed it, nothing changed.
Or also for inline style sheets?
Answer
Yahoo Answer
Answer Man · 6 years ago
It means that the code inside the <style type="text/css"> and end </style> tags is css coding, if there is nothing in it, then nothing will change.
Newer browsers don’t necessarily need that but older ones do, so if you do not see a change, there might be a change on IE 6 and lower.
For inline style sheets you just add it to whatever tag, i.e.
<p style="color:blue;padding:5px;">
If you remove that style tag the color will go to default and the padding will be removed.
Other Answers
Stackoverflow
The html 5 spec says that the type attribute is purely advisory and explains in detail how browsers should act if it’s omitted (too much to quote here). It doesn’t explicitly say that an omitted type attribute is either valid or invalid, but you can safely omit it knowing that browsers will still react as you expect.
Script
Question
Which is better or more convenient to use:
<script type="text/javascript">...</script>
or
<script>...</script>
Answer
Stackoverflow
Do you need a type attribute at all? If you’re using HTML5, no. Otherwise, yes.
HTML 4.01 and XHTML 1.0 specifies the type attribute as required while HTML5 has it as optional, defaulting to text/javascript. HTML5 is usable today and parts of it are becoming widely implemented, so if you use the HTML5 doctype, <script></script> is valid.
As to what should go in the type attribute, the MIME type application/javascript registered in 2006 is intended to replace text/javascript and is supported by current versions of all the major browsers (including Internet Explorer 9). A quote from the relevant RFC:
This document thus defines text/javascript and text/ecmascript but marks them as “obsolete”. Use of experimental and unregistered media types, as listed in part above, is discouraged. The media types,
- application/javascript
- application/ecmascript
which are also defined in this document, are intended for common use and should be used instead.
However, IE up to and including version 8 doesn’t execute script inside a <script> element with a type attribute of either application/javascript or application/ecmascript, so these are both unusable for the foreseeable future and we’re stuck with text/javascript.
The SCRIPT element
Specification
W3C
http://www.w3.org/MarkUp/
http://www.w3.org/html/
http://www.w3.org/standards/
Style
http://www.w3.org/TR/REC-html40/present/styles.html#edef-STYLE
Script
http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1
html4
http://www.w3.org/TR/html4/interact/scripts.html
http://www.w3.org/TR/html4/present/styles.html
xhtml
http://www.w3.org/TR/xhtml-basic/
http://www.w3.org/TR/xhtml1/
http://www.w3.org/TR/xhtml11/
html5
http://www.w3.org/TR/html5/
3 thoughts on “JavaScript and CSS Type”
Christen March 17, 2015 at 3:32 pm
You don’t really need it today, because the current standard makes it optional — and every useful browser currently assumes that a style sheet is CSS, even in versions of HTML that considered the attribute “required”.
Replyhttp://stackoverflow.com/a/7715977
Christen March 17, 2015 at 3:41 pm
Using <style> tags in the <body> with other HTML
ReplyClick Here
Christen March 17, 2015 at 3:53 pm
MSDN MIME types and stylesheets
ReplyClick Here
Test URL