简体   繁体   中英

<script type=“text/javascript”>

Is there any difference between

<script type="text/javascript">

and

<script>

tags ?

The type attribute is no longer a required attribute in most browsers.

From MDC :

type

This 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. If this attribute is absent, the script is treated as JavaScript.

There is no difference between those two tags. Prior to HTML5 you need to explicitly add 'text/javascript'. On HTML5 you can skip that part.

Most browsers will default type to text/javascript , but other values are allowed, see

http://www.w3schools.com/tags/tag_script.asp

Also, as for W3C specifications

As HTML does not rely on a specific scripting language, document authors must explicitly tell user agents the language of each script. This may be done either through a default declaration or a local declaration.

As per W3C standard its essential. As HTML does not rely on a specific scripting language, document authors must explicitly tell user agents the language of each script. This may be done either through a default declaration or a local declaration.

According to CSS-Tricks :

<script type="text/javascript">
  //some javascript here
</script>

The type attribute is the standard and correct way to identify and tell the browser what kind of script the tag contains. Sometimes you'll see code that uses both the language and type attribute. As far as I know that's never necessary.

Really specific explanation from the spec, language is an "obsolete but conforming" feature.

but

<script>
  //some javascript here
</script>

No attributes at all. This is the HTML5 way of handling script tags that contain JavaScript. It's just assumed that the type is text/javascript. If it's not (I've never even seen a different type of script) you'll need to change it with the type attribute. I recommend this is you are using HTML5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM