Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery isn't loading and '$' is undefined in IE8

I am trying to do a very basic thing with jQuery, and it's not working at all in IE8 but works fine in Firefox, Safari and Chrome; IE gives me an "Object Expected" all the time.

Can anyone help? I'm not sure what is wrong as the page is very basic:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>
    <script type="text/javascript">
        $(function() { 
            alert("It works!");
        });
    </script>
</head>
<body>
</body>
</html>

IE (and only IE) gives me an error saying "Object Expected" on the loading code. I have the IE Dev Toolbar and $ is undefined but in Firebug it comes up as "function". really don't know what is causing this.

like image 756
Allan Wight Avatar asked Nov 26 '22 19:11

Allan Wight


2 Answers

I use the HTML5Boilerplate method of:

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if necessary -->
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
  <script>window.jQuery || document.write("<script src='js/libs/jquery-1.5.1.min.js'>\x3C/script>")</script>

EDIT:

Btw ... I use this from IE6-IE9, Firefox, Chrome, Safari, and Opera and I don't ever have an issue with $ being undefined. If you have plugins, I would use this pattern to ensure variables are what they should be:

(function($, window, document, undefined) {

    // code here

})(jQuery, this, document);
like image 188
Code Maverick Avatar answered Dec 09 '22 19:12

Code Maverick


Well, I feel like an idiot. I don't know how (I certainly didn't change it!) but somehow my IE was disabling JavaScript, so no wonder it was not working! I only use FireFox when I can help it.

like image 35
Allan Wight Avatar answered Dec 09 '22 17:12

Allan Wight