0

This is not meant to start a religio-technical browser war - I still prefer Chrome, at least for now, but:

Because of a perhaps Chrome-related problem with my web page (see https://code.google.com/p/chromium/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Iteration%20ReleaseBlock%20Cr%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=&id=161473), I temporarily switched to IE (10) to see if it would also view the time value as invalid.

However, I didn't even get to that point - IE stopped me in my tracks before I could get there; but I found that IE was right - it is more particular/precise in validating my code. For example, I got this from IE:

SCRIPT5007: The value of the property '$' is null or undefined, not a Function object 

...which was referring to this:

<script src="/CommonLogin/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    // body sometimes becomes white???? with jquery 1.6.1
    $("body").css("background-color", "#405DA7");
<

This line is highlighted as the culprit:

$("body").css("background-color", "#405DA7");

jQuery is referenced right above it - so why did it consider "$" to be undefined, especially when Chrome had no problem with it...ah! I looked at that location (/CommonLogin/Scripts/) and saw that, sure enough, the version of jQuery there was actually jquery-1.6.2.min.js. I added the updated jQuery file (1.9.1) and it got past this.

So now the question is: why does Chrome ignore this? Does it download the referenced version from its own CDN if it can't find it in the place you specify?

IE did flag other errs after that, too; so I'm thinking perhaps IE is better at catching lurking problems than, at least, Chrome is. Haven't tested Firefox diesbzg yet.

  • 1
    This question is a bit confusing at the moment. In any case, is it possible that Chrome was using a cached version of jquery? – Steven Burnap Aug 9 '13 at 20:24
  • It's been working this way for several weeks, and I shut off the computer every night (I don't know if that clears the cache or not). – B. Clay Shannon Aug 9 '13 at 21:08
  • If the file was not resolvable then wouldn't you have all sorts of 404 errors - in the console tab and network tab in Chrome's developer tools? – WSkid Aug 10 '13 at 8:24
  • 2
    The web developer's mantra, "If it works in IE, but not the other browsers, your code is wrong." You can never trust IE to do anything right. Eventually, you'll find out the issue was with your code or Chrome was right in the first place. – Rob Sep 12 '13 at 1:43
  • 2
    @ClayShannon IE11 already trails behind all other browsers and it's not even out yet. And then, there it will sit, for at least another year while every other browser moves the web forward every 6-8 weeks. I haven't compiled my list for that one yet. – Rob Sep 14 '13 at 13:16
1

You were using an old version of jquery, as you admitted. The browser detection in that version was broken and so didn't define the global '$' jquery object. So when you tried to use it, it was not defined. You could put the non minified version of the js (the file without the 'min' part) and see where the $ is getting defined (or not getting defined).

Now getting onto Phil's answer, it's right but for the wrong reasons. He is saying you need to wait until dom ready by going $(document).ready. The only reason you need to do that is because you have your javascript in the head. Move it all to the bottom (which is better for speed, dom parsing). So you should have this as it will perform better.

<html>
<head>
<title>Test</title>
</head>
<body>

    <p>Test</p>

    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
   <script type="text/javascript">    
       // no need to wait for ready since we are AFTER the body
     $("body").css("background-color", "#405DA7");

</body>
</html>

Other problems could be that the jquery file itself was sent mangled via gzip incorrectly. Another issue IE 11 has. This would result in $ not defined.

This has nothing to do with $(document).ready

1

before you call $, which is the variable for jquery, you need to let the page load first, then call any jquery object:

  $(document).ready(function () {

      $("body").css("background-color", "#405DA7");

    });

"Javascript statements are executed by the browser immediately and not when the DOM is ready." - See Pro jquery by Adam Freeman [http://www.apress.com/9781430240952] page 107

If you run the following In Firefox, Chrome and IE, it works. The background changes

<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
   <script type="text/javascript">    
              $(document).ready(function () {

                   $("body").css("background-color", "#405DA7");

              });
   </script>    
</head>
<body>

    <p>Test</p>
</body>
</html>

If you remove the line: $(document).ready(function () {..} and only have:

$("body").css("background-color", "#405DA7");

It will not work.

  • 4
    This wouldn't be any different. The $ is used regardless of $(document) vs $("body"). – Joel Aug 9 '13 at 20:39
  • 1
    @Phil Joe I'd right. Wrapping jQuery code in jQuery's domready event won't solve the problem because the domready function is part of the jQuery library. – Rob W Aug 9 '13 at 23:50
  • 1
    I understand page rendering and event binding, but a CSS directive is not an event binding. It's a nonstarter anyway because we're talking about $ being defined; it doesn't matter whether we use a selector or $(document), $ will be undefined in both cases. That's what I mean. Using $(document) doesn't define $. – Joel Aug 10 '13 at 2:32
  • 2
    @Joel You're mistaking what this answer is explaining. <body> is what doesn't exist when that JS runs, which is why you have to wait for DOM load. That said, this doesn't explain the error in the question, which was about $ itself – Izkata Aug 13 '13 at 14:59
  • 1
    @PhilVallone Not true (unless it's an IE bug). The browser stops rendering (including further <script> tags) when it finds a references to an external JS file. The answer to the OP's question almost certainly involves either caching or Chrome doing something special behind-the-scenes. – Izkata Oct 12 '13 at 19:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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