77

I am trying to get my jQuery functions to work on IE8. I am loading the library from Google's servers (http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js).

The $(function(){}) is never called. Instead, I get an error Object expected. I opened the developer and ran typeof $ in the console, and it came up as undefined.

I have tried going to other sites that I know use jQuery (jquery.com), and those all work, is there something I might be missing here?

7
  • 7
    Without seeing something of your page, this is almost impossible to diagnose. Can you tell if the jQuery library is being loaded and run before your code runs?
    – ijw
    Commented May 18, 2009 at 18:38
  • It seems the problem is with jQuery actually loading. I set 3 breakpoints within the jQuery file, and all of them came up as being invalid because there is no executable code associated with that line.
    – Tim Cosgriff
    Commented May 18, 2009 at 19:10
  • Try changing the mode on IE8 to see if IE7 mode or IE8 Compat mode works. You can do so via the Developer Tools.
    – Jab
    Commented May 18, 2009 at 20:30
  • I had similar problems when trying to load jQuery from a script (by adding a script tag to the header) and calling it in the same script. Looked like some sort of timing issue, with jQuery not being loaded in time. I solved it by using putting a static script tag in the HTML code.
    – Tgr
    Commented May 8, 2010 at 14:39
  • I have the same problem with loading jQuery from Microsoft's CDN - my solution works in Chrome, but it doesn't work in IE8. Commented May 27, 2010 at 9:44

20 Answers 20

42

Write "var" before variables, when you define them. IE8 dies when there is no "var".

4
  • Without "var" declarations work in IE7 and IE9. Its hard to understand, how could MS miss such a bug?
    – Neo
    Commented Aug 8, 2011 at 13:41
  • Thanks guys for the answer... You guys saved my head today :)
    – Ravi
    Commented May 21, 2012 at 14:14
  • 3
    Sorry but this is not true. We use var to define a variable with local scope. If you define a variable without var, it actually creates a property with global scope, i.e. as a property of the window object. See this question for more information: stackoverflow.com/questions/1470488/… Commented Sep 5, 2012 at 18:49
  • You saved my day. Thank you! This actually solved my problem with IE10. Now my jQuery mobile website works like a charm in all major browsers :)
    – casaout
    Commented Jan 2, 2013 at 7:56
39

Correction:

Check your script include tag, is it using

type="application/javascript" src="/path/to/jquery" 

change to

type="text/javascript" src="/path/to/jquery" 
1
  • This is the fix for me too Commented Jun 1, 2015 at 19:00
37

I was having a similar issue. Things worked in IE6, Firefox, and IE8 running in IE7 compatibility mode; but not in 'normal' IE8. My solution was to put this code in the header

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 

As to why jquery isn't working in IE8 I'm unclear.

10
  • 19
    that's hideous! it doesn't solve the problem; it just hacks around it, and it'll probably create a whole load of other issues. (IE7-compatibility mode isn't actually completely the same as IE7, it has bugs, quirks and glitches all of its own that don't appear either in normal IE7 nor IE8)
    – Spudley
    Commented Oct 25, 2010 at 12:00
  • 33
    If you have a better solution, please share. Many approaches to get things working in IE are hacks. Commented Oct 25, 2010 at 19:50
  • 8
    If it is a hack, it is one recommended by Microsoft. msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx Commented Dec 14, 2010 at 21:05
  • 11
    Since someone just downvoted this; please tell us why and please highlight your preferred approach. Commented Dec 16, 2010 at 20:58
  • 7
    I had this problem, and I solved it with Richard's solution above. I changed all my <script type="application/javascript" ... to <script type="text/javascript", including my jquery reference to the google cdn, and IE8 started working. I didn't have to throw the whole page into compatibility mode to do it. Commented Nov 1, 2011 at 13:27
12

The solution is to upgrade to the latest version of jQuery. I had the exact same problem and upgraded to 1.4.2 and it all works fine again in IE8.

Seems to be totally backwards-compatible with all the jQuery 1.3.2 stuff I did as well so no complaints here!

1
  • 3
    I'm having this problem with 1.8 for whatever that means.
    – Josh
    Commented Mar 13, 2013 at 22:03
10

I had this problem and tried the solutions mentioned here with no success.

Eventually, I realised that I was linking to the Google CDN version of the script using an http URL while the page embedding the script was an https page.

This caused IE to not load jquery (it prompts the user whether they want to load only secure content). Changing the Google CDN URL to use the https scheme fixed the problem for me.

1
  • i know it's an old post, but you may want to have a look at this - it has a section on the way google now recommends you use its CDN (which gets rid of this issue)
    – stef
    Commented Jan 22, 2013 at 16:54
7

Some people stumbling on this post might get this issue with jquery and IE8 because they're using >= jQuery v2. Use this code:

<!--[if lt IE 9]>
    <script src="jquery-1.9.0.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
    <script src="jquery-2.0.0.js"></script>
<!--<![endif]-->
6

If you are using HTTPS on your site, you will need to load the jQuery library from Googles https server instead. Try this: https://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js (or the latest https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js)

6

jQuery is not being loaded, this is not likely specific to IE8. Check the path on your jQuery include. statement. Or better yet, use the following to the CDN:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js">
</script>
1
  • 3
    Everything works in firefox, safari, and chrome. I can tell that the library is being downloaded, just not executed. I set a breakpoint on the line in the jquery file, and it came up saying that there is no executable code associated with the line.
    – Tim Cosgriff
    Commented May 18, 2009 at 18:56
5

I was fixing a template created by somebody else who forgot to include the doctype.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

If you don't declare the doctype IE8 does strange things in Quirks mode.

1
  • I tried every suggestion above ... changing the doctype (from HTML5's "<!DOCTYPE html>" finally got it working. (In Dev Tools, it shows that the element's class actually changes, but my guess is that IE8's rendering engine doesn't refresh upon changes to the DOM for unrecognized doctypes). Commented Aug 7, 2013 at 13:48
4

The error Object expected is raised because Jquery is not loaded. This happens because of browser security (usually IE) which does not allow you executing external javascript source code. You can correct this problem by:

  • 1: Changing browser security level to allow executing external javascript code. You can find how to do this here

OR

  • 2: Copy-paste the jquery source code into your web page so that it won't be considered as an external script.

I prefer the first solution.

0
4

The onload event does not always work on IE7/8 in <head> ... </head>

You can force it by adding an onload script at the end of your page before the tag as below.

  <script>
    window.onload();
  </script>
</body>
2

Maybe you insert two scripts,it should be work.

<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE8.js"></script>  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js">/script> 
2

This fixed my issue in IE8:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

Running on localhost, I had to change the https:// to http://

If I try to browse to the secure link, I get the Internet Explorer cannot display the webpage friendly warning.

Always try to load your text scripts in a browswer first if there are issues!

2

I had the same problems.

I solved it verifying that IE8 was not configured correctly to reach the SRC URL.

I changed this, it works right.

1

Maybe you have inPrivate Filtering turned on?

1

I had the same issue. The solution was to add the link to the JQuery file as a trusted site in IE.

1

I think that you have same problem as I do:

Message: Permission denied
Line: 13
Char: 27021
Code: 0
URI: http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.3.2.min.js

Because of cross domain reference. Try to host jquery.js on same domain.

0

In short, it's because of the IE8 parsing engine.

Guess why Microsoft has trouble working with the new HTML5 tags (like "section") too? It's because MS decided that they will not use regular XML parsing, like the rest of the world. Yes, that's right - they did a ton of propaganda on XML but in the end, they fall back on a "stupid" parsing engine looking for "known tags" and messing stuff up when something new comes around.

Same goes for IE8 and the jquery issue with "load", "get" and "post". Again, Microsoft decided to "walk their own path" with version 8. Hoping that they solve(d) this in IE9, the only current option is to fall back on IE7 parsing with <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />.

Oh well... what a surprise that Microsoft made us post stuff on forums again. ;)

0

The solution in my case was to take any special characters out of the URL you're trying to access. I had a tilde (~) and a percentage symbol in there, and the $.get() call failed silently.

0

OK! I know that jQuery is loading. I know that jQuery.textshadow.js is loading. I can find both of the scripts in Developer Tools.

The weird part: this code is working in the content area but not in the banner. Even with a dedicated fixIE.css. AND it works when I put the css inline. (That, of course, messes up FireFox.)

I have even put in a conditional IE span around the text field in the banner with no luck.

I found no difference and had the same errors in both jquery-1.4.2.min.js and jquery-1.2.6.min.js. jquery.textshadow.js was downloaded from the jQuery site while trying to find a solution to this problem.

This is not posted to the Website