or ", "text": "\r\n<p>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 <code>type</code> attribute as required while HTML5 has it as optional, defaulting to <code>text/javascript</code>. HTML5 is now widely implemented, so if you use the HTML5 doctype, <code>&lt;script&gt;...&lt;/script&gt;</code> is valid and a good choice. </p>\n\n<p>As to what should go in the type attribute, the MIME type <code>application/javascript</code> registered in 2006 is intended to replace <code>text/javascript</code> and is supported by current versions of all the major browsers (including Internet Explorer 9). A quote from the relevant RFC:</p>\n\n<blockquote>\n <p>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,</p>\n\n<pre class="prettyprint"><code> * application/javascript\n * application/ecmascript\n</code></pre>\n \n <p>which are also defined in this document, are intended for common use and should be used instead.</p>\n</blockquote>\n\n<p>However, IE up to and including version 8 doesn't execute script inside a <code>&lt;script&gt;</code> element with a <code>type</code> attribute of either <code>application/javascript</code> or <code>application/ecmascript</code>, so if you need to support old IE, you're stuck with <code>text/javascript</code>.</p>\n <br>\r\n<p>Both will work but xhtml standard requires you to specify the <code>type</code> too:</p>\n<pre class="prettyprint"><code>&lt;script type="text/javascript"&gt;..&lt;/script&gt; \n\n&lt;!ELEMENT SCRIPT - - %Script; -- script statements --&gt;\n&lt;!ATTLIST SCRIPT\n charset %Charset; #IMPLIED -- char encoding of linked resource --\n type %ContentType; #REQUIRED -- content type of script language --\n src %URI; #IMPLIED -- URI for an external script --\n defer (defer) #IMPLIED -- UA may defer execution of script --\n &gt;\n</code></pre>\n<hr>\n<blockquote>\n<p>type = content-type [CI]\nThis attribute specifies the scripting language of the element's\ncontents and overrides the default\nscripting language. The scripting\nlanguage is specified as a content\ntype (e.g., "text/javascript").\n<strong>Authors must supply a value for this\nattribute. There is no default value\nfor this attribute.</strong></p>\n</blockquote>\n<p>Notices the emphasis above.</p>\n<p>http://www.w3.org/TR/html4/interact/scripts.html</p>\n<p><strong>Note:</strong> As of HTML5, the <code>type</code> attribute is not required and is default.</p>\n <br>\r\n<p>You need to use <code>&lt;script type="text/javascript"&gt; &lt;/script&gt;</code> unless you're using html5. In that case you are encouraged to prefer <code>&lt;script&gt; ... &lt;/script&gt;</code> (because type attribute is specified by default to that value)</p>\n <br>\r\n<p>This is all that is needed:</p>\n\n<pre class="prettyprint"><code>&lt;!doctype html&gt;\n&lt;script src="/path.js"&gt;&lt;/script&gt;\n</code></pre>\n <br>\r\n<p><code>&lt;script type="text/javascript"&gt;&lt;/script&gt;</code> because its the right way and compatible with all browsers</p>\n ", "answerCount": 0, "upvoteCount": 618, "dateCreated": "1970-01-01 00:00:00", "dateModified": "1970-01-01 00:00:00", "author": { "type": "Person", "name": "Admin" } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is the difference if any between script </ script and script type text/JavaScript </ script >?

text/javascript is the default type, so you can omit it, it makes no difference. Nothing, except in very special cases with old IE where if the first script element set the type to some other scripting language (e.g. VBScript) then that was the default if no type was specified.

Should I use type text JavaScript?

No, it is not necessary if you use HTML5. "Differences Between HTML 4.01 and HTML5 In HTML5, the type attribute is no longer required for JavaScript. The default value is "application/javascript"." Source: https://www.w3schools.com/tags/att_script_type.asp To use HTML5 write <!

What is script type text JavaScript?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Is script HTML or JavaScript?

<script>: The Script element. The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code. The <script> element can also be used with other languages, such as WebGL's GLSL shader programming language and JSON.


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.


Both will work but xhtml standard requires you to specify the type too:

<script type="text/javascript">..</script> 

<!ELEMENT SCRIPT - - %Script;          -- script statements -->
<!ATTLIST SCRIPT
  charset     %Charset;      #IMPLIED  -- char encoding of linked resource --
  type        %ContentType;  #REQUIRED -- content type of script language --
  src         %URI;          #IMPLIED  -- URI for an external script --
  defer       (defer)        #IMPLIED  -- UA may defer execution of script --
  >

type = content-type [CI] 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.

Notices the emphasis above.

http://www.w3.org/TR/html4/interact/scripts.html

Note: As of HTML5, the type attribute is not required and is default.


You need to use <script type="text/javascript"> </script> unless you're using html5. In that case you are encouraged to prefer <script> ... </script> (because type attribute is specified by default to that value)


This is all that is needed:

<!doctype html>
<script src="/path.js"></script>

<script type="text/javascript"></script> because its the right way and compatible with all browsers