HTML do I need a mime type for the link tag? - html

David crockford recommends ommitting the type="application/javascript" attribute for the script tag. Should I do the same for a CSS link tag (omit "type=text/css")? I googled "html link omit mime type" and some variants and didn't find anything

Per documentation for <script>:
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".
Now, let's take a look at <link>:
The default value for the type attribute, which is used if the attribute is absent, is "text/css".
The specification is not clear on this for some reason, but it does contain this:
Since that default type is text/css...
The type attribute is also purely advisory. Modern browsers definitely don't need it if it's valid CSS.

There is no practical reason to use the type attribute in either script or link elements, when you are using JavaScript and CSS, as you are (almost always). However, if there is an external requirement imposed upon you to conform to the HTML 4.01 specification, use type="text/javascript" in script, and double-check that you enter it correctly.
Those attributes are never needed (for JavaScript and CSS), but they hurt you if you misspell them. Then browsers will expect that your script is in text/javascript or your style sheet is in text/ccs and ignore it, since they do not know such languages.
In a script element, you would need a type attribute only if the content is not to be interpreted as JavaScript but e.g. as VBScript or not interpreted at all, just stored as data.

Related

Type in script tag of HTML

In Html script tag, we add the type attribute.
<script type="text/JavaScript"></script>
Is there another type of script that is used apart from JavaScript?
type attribute in script tag is used to tell the browser about MIME type or you can say media type. I have also never used different types but I have seen some types i.e.
text/javascript
text/ecmascript
application/ecmascript
application/javascript
By default, it is text/javascript.

Difference between specifying a MIME type and not?

Say I was to write a stylesheet within the HTML document. I know that ever since HTML5 the type attribute is not necessary as text/css has become a default (and only possible) value:
<style type='text/css'></style>
Thus specifying a MIME type is not required but is there any case in which I should specify it?
Also is there a difference between specifying it for the <link> or <style> tag?
<link rel='stylesheet' type='text/css' href='typography.css'>
Technically, it would work either ways but can I omit it in the <link> tag just the way I can in <style> except in the case when the href attribute is set?
In pre HTML5 versions you need to specify it. That is the case, otherwise if you're having an API that would require it, then is the case to specify them. Otherwise since HTML5 you're not asked to always specify them.
Specify them or not, its valid in HTML5.
Omittion
No, a style tag is a tag defined for the Styles of the document. So, Browser would know that you're having styles here. Whereas a link is element defined to link an external source to your document. It can be style and can be a script (JavaScript code).
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
According to the above document,
The HTML Link Element (<link>) specifies relationships between the current document and external resource.
So, to specify you need to declare the MIME type for the resource that you'd link to the current document.

html script links. type = "text/javascript"

Where does the "text" come from? i.e. Why are links described as "text/..."?
I get that you are telling the browser what script to use for script tags.. (is this right?) and that the default language in most browsers is javascript making this line superfluous. However this looks to me like the html is describing a linked file as "a type of text called javascript". Or if you used type = "text/css", then the html is linking to a type of text called css. what else would you use other than "text"?
If we look at W3's documentation on content types, we discover that there are 8 types:
text
application
audio
image
message
multipart
x-token
video
text here is the type and JavaScript, in your example, is the subtype:
Content-Type := type "/" subtype *[";" parameter]
This document then goes on to define what text is:
text: textual information.
The primary subtype, "plain", indicates plain (unformatted) text. No special software is required to get the full meaning of the text, aside from support for the indicated character set. Subtypes are to be used for enriched text in forms where application software may enhance the appearance of the text, but such software must not be required in order to get the general idea of the content. Possible subtypes thus include any readable word processor format. A very simple and portable subtype, richtext, is defined in this document.
text/javascript itself is actually obsolete, and application/javascript should be used instead (however not all browsers will understand what application/javascript is, so the former is much more common).
In HTML5, the type attribute is optional on the script element, and omitting it is perfectly fine:
<script src="myScript.js"></script>
The file is expressed in text as opposed to, for instance, a JPEG.
That said, the text/javascript MIME type is deprecated in favour of application/javascript since the format isn't primarily intended to be read as text (unlike HTML, which is text with markup around it).
The other registries are:
application
audio
example
image
message
model
multipart
text
video

Is type="text/css" mandatory on the <link> element?

I'm trying to use Google Web Fonts, and on the official site it recommends using a <link> tag including the type attribute, as following:
<link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700' rel='stylesheet' type='text/css'>`
In the official HTML5 boilerplate site they ommit the type attribute
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400,700">
According to this rather old answer, in HTML5 the type attribute is optional on the <style> tag and mandatory on the <link> tag.
However, the version without the type attribule validates fine on the W3C validator.
So, is the type attribute mandatory or not?
I found the answer on the official W3C HTML5 draft:
The type attribute gives the MIME type of the linked resource. It is
purely advisory. The value must be a valid MIME type.
For external resource links, the type attribute is used as a hint to
user agents so that they can avoid fetching resources they do not
support. If the attribute is present, then the user agent must assume
that the resource is of the given type (even if that is not a valid
MIME type, e.g. the empty string). If the attribute is omitted, but
the external resource link type has a default type defined, then the
user agent must assume that the resource is of that type. (...)
User agents must not consider the type attribute authoritative — upon
fetching the resource, user agents must not use the type attribute to
determine its actual type. Only the actual type (...).
The stylesheet link type defines rules for processing the resource's
Content-Type metadata. (...)
If a document contains style sheet links labeled as follows:
<link rel="stylesheet" href="A" type="text/plain">
<link rel="stylesheet" href="B" type="text/css">
<link rel="stylesheet" href="C">
...then a compliant UA that supported only CSS style sheets would
fetch the B and C files, and skip the A file (since text/plain is not
the MIME type for CSS style sheets).
For files B and C, it would then check the actual types returned by
the server. For those that are sent as text/css, it would apply the
styles, but for those labeled as text/plain, or any other type, it
would not.
If one of the two files was returned without a Content-Type metadata,
or with a syntactically incorrect type like Content-Type: "null", then
the default type for stylesheet links would kick in. Since that
default type is text/css, the style sheet would nonetheless be
applied.
For the <style> attribute, the same document states:
The type attribute gives the styling language. If the attribute is
present, its value must be a valid MIME type that designates a styling
language. The charset parameter must not be specified. The default
value for the type attribute, which is used if the attribute is
absent, is "text/css". [RFC2318]
I think it is not mandatory, I have successfully used <style> and also <style type="text/css">.. Anything u use will be working such as <script> and <script type="text/javascript"> work the same way :)

How does the browser knows how to interpret the script tag?

According to this:
http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html
The type of script my be added in the script tag. Some values are: "text/tcl", "text/javascript", "text/vbscript".
Recently I've seen in this page: Cofeescript in 1,2,3 the following:
<script src="coffee-script.js"></script>
<script type="text/coffeescript">
alert "Hello CoffeeScript!"
</script>
And works great! ( I had to download the cofeescript library and use the one in the extra folder )
My question is. How does the browser knows that a given script should be handled? I have no idea.
Seeing as you can't finish your answer, it's not yet entirely clear what your question is ;)
But the answer to this question is related: The type attribute of SCRIPT and STYLE elements in HTML?
Summary:
type is indeed a required attribute in HTML 4
it defaults to text/javascript in HTML 5
as far as I know, text/javascript is the de facto default in all modern browsers if the property is missing even in HTML 4.
Since HTML5, the type attribute is optional (it is required in HTML4 though) and the default value is text/javascript.
The browser simply recognises some specific types of script, and ignores everything else.
Internet Explorer for example recognises the type "text/javascript" and runs the script, eventhough it actually runs it as JScript.
I believe that the default in browsers is to interpret a script tag as holding JavaScript; whilst the spec that you've listed indicates that there is no default value for the type attribute, that doesn't mean that browsers won't provide their own default.