10

I developed an HTML page that uses jQuery and Ajax. I had the following lines in my <head> tag:

<script language="javascript" type="application/javascript" src="script/jquery.js"></script>
<script language="javascript" type="application/javascript">
    function someFunction() {
        some code;
    }
</script>
    ...

  later - down in the body tag:
<a href="javascript:someFunction();">click here</a>

It worked perfectly in FF and Chrome, but threw an "Object Expected" in IE8 as soon as I clicked the link. Even with the Script Debugger on, it would not reach the first line of the function.
I tried switching to IE7 mode, or IE8 Compatability mode - to no avail.

I lucked out and found a question through Google where someone suggested changing the type in the <script> tag to "text/javascript" and now it works in all 3 browsers.

My question is: what did I do wrong? Is "text/javascript" better than "application/javascript", or is there a better (== more correct) solution to my issue?

2
  • Thanks for all the correct answers - had to choose one. Would have marked them all as correct if I could :) Aug 17, 2009 at 14:55
  • 2
    I just stumbled across this little stinker when porting a FF-only webapp to IE. WHY MUST IE BE MUST A MISERABLE BROWSER
    – Matt Ball
    May 25, 2010 at 17:23

2 Answers 2

22

"text/javascript" is the only type that is supported by all three browsers. However, you don't actually need to put a type. The type attribute of a script tag will default to "text/javascript" if it is not otherwise specified. How that will affect validation, I'm not sure. But does that really matter anyway?

2
  • The validator definitely complains if you leave it off Feb 28, 2013 at 12:08
  • @TimSeguine depends on the doctype. In HTML5 it's all good without the type attribute. Feb 11, 2014 at 11:29
2

Simple answer is that IE doesn't support the type value of application/javascript. The RFC 4329 which recommends it obviously came after the release of IE6, I guess the MS developers didn't feel the need to even support the MIME type.

3
  • 1
    Indeed. There's some discussion about whether that should change, but obviously that would make it easy for developers to inadvertantly build sites that don't work in 60+% of the world's browsers.
    – EricLaw
    Aug 17, 2009 at 14:40
  • Recently already a bit less than 60% of the world's browsers. Yay!
    – macbirdie
    May 5, 2010 at 9:35
  • What's really odd is that that the application/javascript RFC came out in 2006, and IE8 came out in 2009. I guess it was part of their old fracture the web strategy, but with slumping browser shares they are really going to be in it deep now. Thank you chrome and firefox!
    – Brad C
    Jul 20, 2012 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.