JQuery does not load, and "$" is undefined in IE8

I am trying to make a very simple case with jQuery and it does not work at all in IE8, but works fine in Firefox, Safari and Chrome; IE gives me the "Object Expected" all the time.

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

<!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 the "Object Expected" error message in the download code. I have the IE Dev toolbar, and it $is undefined, but in Firebug it appears as a "function". really don't know what causes this.

+5
source share
6 answers

I use the HTML5Boilerplate method :

<!-- Grab Google CDN 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... IE6-IE9, Firefox, Chrome, Safari Opera, $be undefined. , , , :

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

    // code here

})(jQuery, this, document);
+1

, . , (, , !), - IE JavaScript, , ! FireFox, .

+1

. , , , "jQuery" .

jQuery(function() { 
            alert("It works!");
});
0

:

(function($){
    $(function(){

    })
})(jQuery)
0

Internet Explorer 8.0.7600.16385 Windows 7.

- .

-1
source

You probably need to place your function in the finished event handler as follows:

$(document).ready(function () {
   $(function() {
     alert("It works!");
   }):
});

Thus, the entire domain was processed before the launch of your function.

-5
source

All Articles