What is the difference in various ways of defining a script?
What is the difference in various ways of defining a script?
The ways I am talking about is this
<script>....</script>
<script language="javascript">.....</script>
<script type="text/javascript">...........</script>
Since, they all开发者_如何学Python do same things, what is different?
According to w3c spec, type
attribute is required, and determines script language, whereas language
attribute (which does more or less the same) is deprecated in favor of type
, so you should use type
attribute.
<script language="javascript">
HTML 3.2 — The first attempt
<script type="text/javascript">
HTML 4.x and XHTML 1.x — using MIME types for everything. This is the current standard. Use this.
<script>
HTML 5 (draft) — "Ah, too many authors don't care, browsers error recover from it anyway, lets all but give up on the possibility other other embedded scripting languages being used"
In HTML 4.01 the type attribute is required. http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-type-SCRIPT
In practice though, all commonly used browsers default this to text/javascript, so there isn't really a need to specify it unless you care about validators or older versions of IE. The HTML5 spec makes it optional. http://www.whatwg.org/specs/web-apps/current-work/#attr-script-type
精彩评论