<Script> Tag VS <Script Type = 'Text/Javascript'> Tag

Which is better: script type=text/javascript.../script or script.../script

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 now widely implemented, so if you use the HTML5 doctype, <script>...</script> is valid and a good choice.

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 if you need to support old IE, you're stuck with text/javascript.

script tag vs script type = 'text/javascript' tag

In HTML 4, the type attribute is required. In my experience, all
browsers will default to text/javascript if it is absent, but that
behaviour is not defined anywhere. While you can in theory leave it
out and assume it will be interpreted as JavaScript, it's invalid
HTML, so why not add it.

In HTML 5, the type attribute is optional and defaults to
text/javascript

Use <script type="text/javascript"> or simply <script> (if omitted, the type is the same). Do not use <script language="JavaScript">; the language attribute is deprecated

Ref :

http://social.msdn.microsoft.com/Forums/vstudio/en-US/65aaf5f3-09db-4f7e-a32d-d53e9720ad4c/script-languagejavascript-or-script-typetextjavascript-?forum=netfxjscript

and

Difference between <script> tag with type and <script> without type?

Do you need type attribute at all?

I am using HTML5- No

I am not using HTML5 - Yes

Difference between script type and script language declarations

Use <script type="text/javascript"> or simply <script> (if omitted, the type is the same). Do not use <script language="JavaScript">; the language attribute is deprecated.

What are modern uses of script type=text/html and is this example considered good use?

According to the HTML5 spec for the script tag, it's totally fine to use <script> with a type attribute set to any valid MIME type. That includes MIME types like text/html or text/plain.

According to the HTML4 spec for the script tag, it's not quite fine:

"There are two types of scripts authors may attach to an HTML
document: Those that are executed one time when the document is loaded
[and t]hose that are executed every time a specific event occurs"

You don't need backbone for templating. You can use e.g. jQuery or my personal favorite, Mustache.js.

Do you need text/javascript specified in your script tags?

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.

language=javascript vs. type=text/javascript

<script language="javascript"> was used in very old browsers, and is deprecated.

<script type="text/javascript"> is the HTML 4 standard.

In HTML 5, the type parameter is optional (text/javascript is the default), so you can just do <script>.

As a neat hack, if you put an invalid type, the script won't be ran, but you can still read the data in JavaScript. Some template libraries do this.

What difference between tag template and script type = 'text/template'?

<template> is used to define reusable snippets of HTML that Javascript can use. You would define it with an id, and then use it.

<script type='text/template'> escapes the normal HTML processor because text/template is not a valid MIME type. Thus is outputs as plain-text. Learn more here: Explanation of <script type = "text/template"> ... </script>

EDIT:

text/html indicates that the HTML processor should be applied, formatting all HTML tags and the like.

Explanation of script type = text/template ... /script

Those script tags are a common way to implement templating functionality (like in PHP) but on the client side.

By setting the type to "text/template", it's not a script that the browser can understand, and so the browser will simply ignore it. This allows you to put anything in there, which can then be extracted later and used by a templating library to generate HTML snippets.

Backbone doesn't force you to use any particular templating library - there are quite a few out there: Mustache, Haml, Eco,Google Closure template, and so on (the one used in the example you linked to is underscore.js). These will use their own syntax for you to write within those script tags.

Is the 'type' attribute necessary for script tags?

For HTML 4.x, the type attribute is required. Source

This 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.


For HTML 5, it is optional. If it is not specified, it defaults to text/javascript. Source

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".

Recommendation: See HTML 5.2


For HTML 5.2, it should be omitted if using a valid JavaScript MIME type (e.g. text/javascript). Source

Omitting the attribute, or setting it to a JavaScript MIME type, means that the script is a classic script, to be interpreted according to the JavaScript Script top-level production. Classic scripts are affected by the charset, async, and defer attributes. Authors should omit the attribute, instead of redundantly giving a JavaScript MIME type.



Related Topics



Leave a reply



Submit