vol7ron
vol7ron

Reputation: 42109

MIME TYPE: Linking to CSS w/ a txt Extension

<link type="text/css" media="all" rel="stylesheet" href="./foo.css.txt" />


Firbug Error


The stylesheet foo.css.txt was not loaded because its MIME type, "text/plain", is not "text/css".


Explanation


The browser is interpreting the CSS as a text document rather than a CSS, despite any effort to force it's MIME through the browser. Naturally, in production I will be using foo.css (w/o the txt extension), but I would think this would be possible. It works for JavaScript:

<script type="text/javascript" src="./index.js.txt"></script> 


Other Thoughts


Upvotes: 3

Views: 5964

Answers (4)

Greg Ball
Greg Ball

Reputation: 3811

In case anyone still wants to do this, it can be done with JavaScript. Here's a rough jQuery solution:

var sheet = $('<style></style>')
$('head').append(sheet)
sheet.load("./foo.css.txt")

Upvotes: 2

Quentin
Quentin

Reputation: 943615

When you use the type attribute in HTML you are saying "When you fetch this resource, expect to get this type of data." This allows user agents (such as browsers) to avoid requesting resources that they know they can't handle. For CSS you need to set this to text/css.

When the server responds to the request, the HTTP Content-Type header says "This content is of this type". It is canonical and must also be set to text/css for CSS data.

Because you have used a .txt file, the server (in a default configuration) says that the file contains data that is text/plain so it tells the browser that. You cannot override this from the browser.

It works for JavaScript

Browsers, for various reasons, tend to ignore the Content-Type specified by servers for JavaScript. This could be considered a bug in those browsers.

Setting type="css" doesn't show an error in Firebug and the page validates, but the CSS doesn't load properly - I'm pretty sure that's not a valid MIME type though

css is not a registered MIME type, the browser doesn't recognise it as a stylesheet language that it can understand, so it doesn't event try to download it.

I tried to find a list of MIME types off W3C, but as with everything it's difficult to find/understand W3C

The W3C isn't responsible for the MIME type registry, which you can find at http://www.iana.org/assignments/media-types/

Upvotes: 6

RDL
RDL

Reputation: 7961

Try using a server side extension like .php

In this case you can set the mime-type in the document itself.

Upvotes: 0

Damien
Damien

Reputation: 1217

If you could use .php instead of .txt you could set type from within the file itself.

Upvotes: 0

Related Questions