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

JavascriptHtml

Javascript Problem Overview


Which is better or more convenient to use:

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

or

<script>...</script>

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - 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.

Solution 3 - Javascript

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)

Solution 4 - Javascript

This is all that is needed:

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

Solution 5 - Javascript

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

Solution 6 - Javascript

For HTML5, <syntax>...</syntax> is better and more convenient to use. If you are using HTML5, there is no need to mention type = "text/javascript" explicitly as type attribute is set to "text/javascript" by default, so it is completely optional.

Solution 7 - Javascript

With the latest Firefox, I must use:

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

Or else the script may not run properly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpencilCakeView Question on Stackoverflow
Solution 1 - JavascriptTim DownView Answer on Stackoverflow
Solution 2 - JavascriptSarfrazView Answer on Stackoverflow
Solution 3 - JavascriptfcalderanView Answer on Stackoverflow
Solution 4 - JavascriptchovyView Answer on Stackoverflow
Solution 5 - JavascriptpoojaView Answer on Stackoverflow
Solution 6 - JavascriptAkshay KatihaView Answer on Stackoverflow
Solution 7 - JavascriptStephen ElliottView Answer on Stackoverflow