The MIME type
Unknown data type – now what?
In order to sensibly use data, it is helpful to know what one is dealing with.
This doesn't pose a problem when files are downloaded directly, because one can
determine their probable nature by the file's extension, but when the data is
provided by a script or the likes, things look much differently. In this case
the only thing that helps would be guesswork what type of data one is dealing
with. Attempting to determine the nature of the data from what has been
received quickly proves to be prone to errors, and that which is working
reasonably well for text files is
normally going to fail as soon as binary
data is passed.
Since the transmitted data doesn't contain any header data that would permit
recognizing what one is dealing with, the only thing that the receiving program
could do is saving the downloaded file to disk so that the user may have a look
on what he is dealing with – which would far from practical – or launch a
costly data analysis in terms of computation time to perhaps determine the data
type that it is dealing with.
But no matter which approach is chosen, in terms of usability both are a no-go.
What the MIME type is telling us
To cope with this problem,
a particular entry is passed along with
the other header data that tells the recipient what it is dealing with so that
it may immediately determine what to do with this data. Providing it has the
necessary means at its disposal it is going to directly process the data, or if
it is able to access an appropriate extension, pass the data on. Only when the
recipient encounters an unknown data type the recipient is going to offer the
user a download.
When examining the MIME type of a data entity one is going to notice that it is
composed of two identifiers separated by a slash, e. g.
text/html. The part before the slash determines the
general data type (text data in this case), and what has been put after the
slash makes it more specific. In this case it's HTML data that, when loaded
into a browser, tell it how to render the page it describes.
A file that is instead transmitted as image/jpeg is
recognized as JPEG file by the browser which it subsequently displays either
all by itself when referenced by a link or as part of an HTML document when
said file is included as an image.
text/plain
This is exactly what the MIME type describes: Plain text data. The browser is
going to display such a file without further processing exactly how it resides
on the server, i. e. with all line breaks, etc. exactly at those spots where
they are within the text file.
One can of course transmit any other data than plain text data in conjunction
with this MIME type, but in this event they are presented in their raw form.
An HTML file that is for example transmitted in this manner is going to appear
in its original form, i. e. exactly how it resides on disk.
You are normally not going to encounter this MIME type, except that a text file is offered to you that you are supposed to save to disk. Creating a web page in this fashion turns out to be a fruitless endeavor since you have to define the layout within the text file itself and are able to integrate neither links nor any other files.
to the toptext/html
You are going to encounter this MIME type whenever you are calling a web page.
This tells the browser that the file isn't an ordinary text file, but also
contains additional formatting information that allow it to correctly render
the page.
HTML document validation unfortunately is very lax, which could induce someone
to write problematic HTML code that may nevertheless be rendered by a browser,
but which contains syntax and logical errors – which leads to different
browsers (even different versions within the same browser family) rendering the
faulty code entirely differently.
To mitigate this problem it is highly recommended to use a validator service.
application/javascript
This MIME type shows up any time that a JavaScript that is to be integrated
into an HTML document is loaded from an external source. It instructs the
browser to execute the code contained in the file.
It must be kept in mind, though, that JavaScript shares a common namespace even
though it is split into multiple files. This must be kept in mind here,
especially when multiple files introduce identifiers of the same name.
However, quite often one can still find the MIME type text/javascript, but it is considered obsolete and therefore shouldn't be used. To transfer files containig JavaScript application/javascript is to be used instead.
to the toptext/css
This denotes a cascading style sheet that contains layout information for the
browser so that it can correctly display the HTML file that includes it. The
browser is generally going to sequentially display the elements it finds in the
HTML file, but more complex page layouts may require the elements to be
displayed in another order than has been defined in the HTML file.
However, since the HTML code (that defines what the
browser is supposed to display) and the layout information (that defines
how the browser is supposed to display the various
elements) have been split apart, it is necessary to put this layout information
somewhere. These may be defined centrally in files – which in turn are
transferred using this MIME type – that are integrated by every document so
that all pages are using the same layout information.
application/xhtml+xml
This MIME type finally specifies text files, but the manner in how they are
processed displays some peculiarities. The XHTML thus identified is, in
contrast to HTML which has been derived from SGML, a derivative of XML, and
furthermore the transferred document isn't considered to be text albeit
residing on disk in this form, but instead an XML application.
One is furthermore going to notice that the browser is much more pedantic when
executing XHTML than it would be when displaying HTML, because it is going to
display an error message whenever it encounters an error instead of displaying
the page. On the one hand this could constitute a disadvantage since you won't
get around writing valid XHTML code, but on the other hand this is a clear
advantage, because you can be absolutely sure to have written a syntactically
correct document as soon as it is displayed by a browser. Any eventual logical
errors can be easily spotted with the help of a validator service.