08.17.05
Opera, text/javascript, and UTF-8.
I came across what appears to be a minor bug in Opera’s handling of javascript and character encoding. If I send it a stream of text that is accurately described by the header “Content-Type: text/javascript; charset=utf-8″ Opera (at least 8.02) is at a loss for what to do.
When loadng the address directly it just tries to find an external handler for that mime type (and fails for me). If I try to load this bit of javascript through the XMLHttpRequest I get something, however I am not familiar enough with the different east-asian languages to determine what character of beast this is. Suffice it to say that it is not what I intended to get back.
If I change the mime-type to “Content-Type: application/x-javascript; charset=utf-8″ or ‘text/plain’ or ‘text/html’ it works just dandily. Not to difficult of a workaround, but is this really a workaround or “The Proper Way”?
Safari, Firefox, and Internet explorer did not have any problems digesting the ‘text/javascript’. So for trying to follow a de facto standard, which from what I know of Opera they generally try to do, not very good. Most webservers serve ‘application/x-javascript’ so they do have that one in the bag.
This really comes down to: There is no standard javascript mime-type.
A script block with type=”text/javascript” writes: ”
document.write(”foo”);
”
A script block with type=”application/x-javascript” writes: ”
document.write(”bar”);
”
My test showed that both of those work in Mozilla and Opera, and I only get “foo” when in IE.
Gary Court said,
July 11, 2006 at 8:43 am
Hey, thanks for the research! I ran into this same problem yesterday, and it appears that Opera is not treating the utf-8 encoding right for text/javascript. Go figure. I’m just going to use text/plain content-type for DL’ing javascript with XMLHTTPRequest as the workaround for this.