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?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • 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 May 18 '09 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. –  May 18 '09 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 May 18 '09 at 20:30
  • I have the same problem with loading jQuery from Microsoft's CDN - my solution works in Chrome, but it doesn't work in IE8. – Marek Grzenkowicz May 27 '10 at 09:44
  • Also, as in my case, a possible cause is Internet Explorer Enhanced Security Configuration (ESC). Which is turned on by default on a Windows 2008 Server. [To disable see this.](http://4sysops.com/archives/how-to-disable-internet-explorer-enhanced-security-configuration-ie-esc-in-windows-server-2008/) – gerleim Jan 03 '12 at 16:26
  • There is [another way](http://stackoverflow.com/q/1106755/95) to load jQuery from Google CDN (it uses `google.load()` function) and according to [this answer](http://stackoverflow.com/questions/3966994/is-my-jquery-cdn-url-correct/4013446#4013446) it works both in Firefox and Internet Explorer. – Marek Grzenkowicz Oct 25 '10 at 11:54
  • 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 May 08 '10 at 14:39

20 Answers20

42

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

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
asd
  • 429
  • 4
  • 2
  • Without "var" declarations work in IE7 and IE9. Its hard to understand, how could MS miss such a bug? – Neo Aug 08 '11 at 13:41
  • Thanks guys for the answer... You guys saved my head today :) – Ravi May 21 '12 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: http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript – James McCormack Sep 05 '12 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 Jan 02 '13 at 07: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" 
Brad
  • 159,648
  • 54
  • 349
  • 530
Richard
  • 391
  • 3
  • 2
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.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • Works, but I'm still very confused as to why it was broken in the first place! – TimS Sep 20 '10 at 14:42
  • 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 Oct 25 '10 at 12:00
  • 33
    If you have a better solution, please share. Many approaches to get things working in IE are hacks. – JeffryHouser Oct 25 '10 at 19:50
  • 8
    If it is a hack, it is one recommended by Microsoft. http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx – eric.christensen Dec 14 '10 at 21:05
  • 11
    Since someone just downvoted this; please tell us why and please highlight your preferred approach. – JeffryHouser Dec 16 '10 at 20:58
  • 7
    I had this problem, and I solved it with Richard's solution above. I changed all my – Vineel Shah Nov 01 '11 at 13:27
  • 1
    This is dodging the issue by going down to iE7 mode. Look at the other solutions on this page for iE8 issues. – thetoolman Jun 14 '12 at 03:55
  • In your favor: OP didn't post any details to his problems, so anyone just had to guess. But in general, suggesting to switch to IE compat. mode is a very bad thing to do. – wiesion Jun 11 '15 at 14:22
  • @wiesion Why is that a bad thing to do? – JeffryHouser Jun 16 '15 at 20:24
  • IE7 has a host of other issues compared to IE8 (which already gets somewhat close to W3C standards in total contrast to IE7), and on top of that, IE7 compat. mode does not give reliably the same result as a real IE7 (Means you'll be targeting 3 browsers, not 2). For a quick overview: http://www.smashingmagazine.com/2009/10/14/css-differences-in-internet-explorer-6-7-and-8/ – wiesion Jun 17 '15 at 08:29
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!

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.

lmcanavals
  • 2,339
  • 1
  • 24
  • 35
sheltond
  • 101
  • 1
  • 2
  • i know it's an old post, but you may want to have a look at [this](http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/) - it has a section on the way google now recommends you use its CDN (which gets rid of this issue) – stef Jan 22 '13 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]-->
Stuart Dobson
  • 3,001
  • 4
  • 35
  • 37
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)

lhoess
  • 307
  • 3
  • 6
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>
cwharris
  • 17,835
  • 4
  • 44
  • 64
cgp
  • 41,026
  • 12
  • 101
  • 131
  • 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. –  May 18 '09 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.

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
Hone Watson
  • 111
  • 1
  • 2
  • I tried every suggestion above ... changing the doctype (from HTML5's " " 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). – Stephen M. Harris Aug 07 '13 at 13:48
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>
lmcanavals
  • 2,339
  • 1
  • 24
  • 35
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.

Nanne
  • 64,065
  • 16
  • 119
  • 163
zelmarou
  • 231
  • 2
  • 9
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.

Tom van Enckevort
  • 4,170
  • 1
  • 25
  • 40
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> 
Vadim Sluzky
  • 259
  • 1
  • 2
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!

lenny
  • 21
  • 1
1

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

Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101
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.

animuson
  • 53,861
  • 28
  • 137
  • 147
Vanja
  • 11
  • 1
1

Maybe you have inPrivate Filtering turned on?

Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
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.

jorisw
  • 875
  • 1
  • 11
  • 17
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

lmcanavals
  • 2,339
  • 1
  • 24
  • 35
Kyle
  • 9
  • 1
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. ;)

Tim Stone
  • 19,119
  • 6
  • 56
  • 66
e-sushi
  • 11