Is the appropriate type to serve Javascript "text/javascript" or "application/x-javascript" or "application/javascript"?

Asked

Viewed 191 times

12

When analyzing the source code of several pages, it can be seen that the Javascript headers vary with regard to the mime (English) indicated, but the doubt remains about which we should effectively use:

How we use HTML to indicate a javascript block (English)

text/javascript

Denoting application via:

application/x-javascript

or

application/javascript

Question

What the mime correct to send when we are serving Javascript? Post and Cons of your use?

  • Failure to quote when not specified, having only <script></script>

  • 3

    @Orion This case is another "war", here I really mean http-headers, then apply tag ;)

1 answer

8


The "right" type is application/javascript according to the RFC 4329 (text/javascript is obsolete). But modern browsers can understand all of them. The reason why "application" is correct is that the browser will run Javascript code, and it does not need to be read (as text) - for example, a minified JS is not readable (in most cases), but can be run smoothly.

In other words, if you want to be exact, and use the recommended MIME type, then you should use application/javascript. I put the term "correct" above in quotes on purpose - all (as far as I know) modern browsers will treat the response to a request initiated by a <script src=...> as Javascript, regardless of the content-type value in the server response.

However, it may be that in the future some more strict browser is created, or that some security hole is detected and that makes the browsers require the use of the "correct" mime type, so start using the correct type can now save headache later.

  • Hello. Really, I don’t wear any because I never really cared about it. Is there any gain in that or is it just perfumery? Give an enriched answer,

  • If you want to be a pedant, and use the recommended MIME type, then you should use application/javascript. I put the term "correct" in quotes on purpose in the answer - all (as far as I know) modern browsers will treat the response to a request initiated by a <script src=...> as Javascript, regardless of the value of Content-Type in the server response.

  • However, it may be that in the future some more pedestrian browser is created, or that some security hole is detected and that makes the browsers require the use of "correct" MIME, so start using the correct type can now save headache later.

  • 1

    Fair, argument accepted. I think it would be nice if you complemented your answer with these details, just to look good.

  • 1

    I was going to make a comment unrelated to mime-type here, but I’m afraid to sound pedantic.

  • @Edgarmunizberlinck, added the comments to the reply.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.