Do you need text/javascript specified in your <script> tags?
I read somewhere that you no longer need things like type="text/javascript"
and the weird CDATA
and <!--
things in your script tags. So, instead of:
<script type="text/javascript">
//<![CDATA[
<!--
//your script here
-->
//]]>
</script>
You would just do:
<script>
//your script here
</script>
I can't remember where I read this though. It was from a Google or Yahoo engineer I think, and they specifically mentioned which browsers required these archaic constructs and why. Anyone know what blog post/开发者_JAVA百科article this was talked about, or have a good resource talking about this?
See Crockford's write-up on the <script>
tag, most notably:
Do not use the
<!-- //-->
hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years.<!-- //-->
is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include--
, so a script that decrements has an HTML error.
...
type="text/javascript"
This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.
It's a Crockford recommendation. I know I've seen it echoed elsewhere (ppk maybe?). The HTML5 spec does not require it.
Oddly, it's become somewhat au courant to use the "type" attribute to mark <script>
blocks that you don't want to be evaluated:
<script type='text/html-template'>
<div> this is a template </div>
</script>
By giving a weird non-JavaScript type, you get a way to stuff raw text into the page for use by other JavaScript code (which is presumably in script block that can be evaluated).
HTML5 doesn't need the type="text/javascript"
(it's the default).
CDATA
is only neeed for XHTML pages, if the script has any HTML characters (like '<' and '>') in it.
<!--
should only be needed for OLD browsers.
The type attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript.
According to HTML 4.01 Specification
The type 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 (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.
But in HTML5 text/javascript
is the default type, so you can omit
The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".
Well, I am tempted to say that nobody is using text/javascript
any more, and that even minification tools would probably remove it...
Indeed, Facebook SDK documentation specifies just <script>
.
However,
Google SDK documentation still has text/javascript
.
Amazon SDK documentation still has text/javascript
.
Linkedin API documentation still has text/javascript
.
Instagram is still using text/javascript
.
you may be thinking of this article here with the dependency being that scripts default to text/javascript in HTML5 automatically, while non-HTML5 browsers still expect that you define the type specifically spec-wise even though they will almost always guess text/javascript anyways.
精彩评论