This question is about browser behavior as well as protocol specification for linking, importing, including or ajaxing css, js, image and other resources from within html, js or css files.
While testing static files and compressed content delivery in different browsers, I found that some browsers start behaving differently if you move away from conventions. For example, IE6 creates problem if you do not send Content-Disposition: inline;
header for all inline css and js etc files, and a recent version of safari does not properly handle pre-compressed gzip CSS files if you use file extension .gz
like in main-styles.css.gz
.
My question is about the behavior of browsers about Content-Type
response header. Since <link>
, <script>
and <img>
tags already reasonably specify the content type of the resource, can this header be safely skipped, or do some browsers require it for some historical reason?
In short, no, it's not required. But it's recommended. Most browser that I know of will treat
<link>
,<script>
, and<img>
properly if they are not sent with headers, but there's no real good reason not to send the headers. Basically, withoutContent-Type
headers, the browser is left to try and guess based on the content.From RFC2616:
Regarding the keyword SHOULD, specified in RFC2119:
I ran into a problem in java where I tried to post some data via the library chrriis.dj.nativeswing.swtimpl.components.JWebBrowser, which basically displays an internet explorer inside a java program. But the simple php script on the back-end would not parse my post-data. (Used WebBrowserNavigationParameters to set post data while navigating to a certain page) I finally found out that the Content-Type header had to be set for php to properly paste the post-data. (This was not set by default.) Setting it to Content-Type: application/x-www-form-urlencoded and everything worked fine. So, I guess setting Content-Type should allways be done when POSTing data to php.
It is required for backward compatibility.
For example:
Internet Explorer 10
needsContent-Type:image/svg+xml
in order to render any svg fileIE10, IE9 and probably other browsers always need the
Content-Type
header.