2

The question is text/javascript works across the board but application/[x-]javascript is correct. Is there any reason not to use text/javascript?

Background:

I've discovered an interesting corner case where Chrome will refuse to execute Javascript passed as application/x-javascript or application/javascript passed as a utf-8 encoded data url (so data:application/x-javascript;charset=utf-8,...)

Specifically, Chrome allows:

data:text/javascript; charset=utf-8,....
data:application/javascript; charset=utf-8;base64,....
data:application/x-javascript; charset=utf-8;base64,.....

But explodes on:

data:application/x-javascript; charset=utf-8,.....
data:application/javascript; charset=utf-8,.....

Based on googling, it seems as though RFC 4329 dictates application/javascript as the standard, and it works in this case, but so does the (now deprecated) text/javascript.

Chasing down RFC2397, RFC2045, RFC2046 didn't show a definitive answer.

podperson
  • 2,284
  • 2
  • 24
  • 24
  • 1
    Related: [Difference between application/x-javascript and text/javascript content types](https://stackoverflow.com/questions/9664282/difference-between-application-x-javascript-and-text-javascript-content-types) – Jonathan Lonowski Jun 17 '16 at 17:32
  • Yeah I saw that and I know what's documented in the RFC, but I also know that text/javascript works everywhere and some things don't obey RFCs. If there are no significant examples then I'll do the "right thing", but not crashing > correctness. – podperson Jun 17 '16 at 17:36
  • I note that in the linked answer a comment points out that application/javascript will cause SVN to treat your file as binary. (But I only care about browsers.) – podperson Jun 17 '16 at 17:42

1 Answers1

1

First, note that I edited the question after realizing Chrome was broken for application/javascript as well as x-javascript.

It looks like it's a straight-out bug in Chrome. It doesn't refuse to execute the utf-8 "encoded" javascript, it instead collapses spaces causing, for example, any definition of a named function to become incorrect, e.g. function foo(){} becomes functionfoo(){}. This does not happen for text/javascript or base64 encoded uris.

So, the answer is to use text/javascript.

As an aside: it seems kind of precious to insist on deprecating text/javascript while retaining text/css.

podperson
  • 2,284
  • 2
  • 24
  • 24