When serving JavaScript files, is it better to use

2019-01-03 06:18发布

The whole question fits in the title. And to add some context: I'm not asking what is the best according to what the specs are saying, but rather what works the best given the mix of browsers deployed nowadays.

Some data points:

  • Google uses text/javascript for the JS used on their home page.
  • Google uses text/javascript on Google Docs.
  • Google uses application/x-javascript to serve JavaScript files with their Ajax libraries service.
  • Yahoo uses application/x-javascript to serve their JS.
  • Yahoo uses application/x-javascript for the JavaScript served on their home page.

5条回答
Animai°情兽
2楼-- · 2019-01-03 06:28

It used to be language="javacript". Then it changed to type="text/javascript". Now it is type="application/javacript". Ok this is getting dumb. Some of the older browsers don't recognize the new application/javascript, but still recognize the older text/javascript. I plan to continue using this, or else I'll waste hours of my time trying to change EVERY instance of text/javascript into application/javascript.
Now some day the opposite might be true. Some day the newest browsers might reject the old technique in order to be strictly standard's compliant.
But until people viewing my website start complaining that "ever since upgrading my browser, about 50% of your website disappeared", I have no motive to change the code in my website.

查看更多
SAY GOODBYE
3楼-- · 2019-01-03 06:35

If you choose to use application/javascript for js in your pages, IE7 and IE8 will not run your script! Blame Microsoft all you want, but if you want most people to run your pages use text/javascript.

查看更多
Rolldiameter
4楼-- · 2019-01-03 06:37

In most situations, the mime type the server sends makes no practical difference. I would go with application/javascript, which is also recommended by an RFC.

查看更多
做个烂人
5楼-- · 2019-01-03 06:40

It has been "text/javascript" but this is obsolete (see the IANA List) and now it should be "application/javascript" (see another IANA List).

查看更多
smile是对你的礼貌
6楼-- · 2019-01-03 06:53
  • text/javascript is obsolete
  • application/x-javascript was experimental while deciding to move to…
  • application/javascript is the current official MIME type for JS

That said, browsers often ignore the content-type sent by the server and pay a lot of attention to the type attribute (and some may not yet recognise application/javascript).

My recommendation:

  • Use application/javascript on the server
  • Use HTML 5 and omit the type attribute from script elements
查看更多
登录 后发表回答