开发者

'$ is not defined' in code following jQuery include

开发者 https://www.devze.com 2023-02-05 06:48 出处:网络
I\'m getting the familiar \'$ is not defined\' error in my JavaScript for the following line in one of my javascript files...

I'm getting the familiar '$ is not defined' error in my JavaScript for the following line in one of my javascript files...

$(document).ready(function() { … }

Normally this is because I've forgotten to include jQuery, but not this time! I have included it, and it is the first include in the page. This erro开发者_开发技巧r happens in included JavaScript files, and also in any code within tags, all of which come after the jQuery include. It also doesn't happen all the time, maybe half the time when I refresh the page.

I've also used jQuery quite a lot and never seen this before, so I am quite confused.

Edit: Looking at the Net tab in Firebug, jQuery is being requested and I get a 200 response but nothing is sent back in the response body. If I open the file directly in a new tab or whatever, I get an empty document. Firefox seems to think the file is cached but the data size is 0. Cache control is 'no-cache'. Confused.

Edit 2: Opened jQuery file in VisualStudio, saved jQuery file with no modifications, everything works perfectly now. Still totally confused.


Are you sure that jQuery is actually being loaded into the browser? It sounds like it really isn't. You should use Firebug or Fiddler to check all the http requests to see if it is actually being downloaded.

Here's a screenshot of how you can check this using Firebug.

'$ is not defined' in code following jQuery include


Are you using Wordpress or some sort of CMS? If so, their version of jQuery may have of code at the end which calls jQuery.noConflict(). You can read about this method here: http://api.jquery.com/jQuery.noConflict/

This means that whenever you want to use the $ function, you need to use jQuery instead.

For example...

Instead of

$("p").addClass("awesome");

You would do

jQuery("p").addClass("awesome").
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号