views:

1513

answers:

6

I have a simple jquery click event

<script type="text/javascript">
    $(function() {
        $('#post').click(function() {
            alert("test"); 
        });
    });
</script>

and a jquery reference defined in the site.master

<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript"></script>

I have checked that the script is being resolved correctly, i'm able to see the markup and view the script directly in firebug, so i must be being found. However i am still getting...

$ is not defined

and non of the jquery works. I've also tried the various variations of this like $(document).ready and jQuery etc.

It's an MVC 2 app on .net 3.5, i'm sure i'm being really dense, everywhere on google says check the file is reference correctly, which i have checked and checked again, please advise! :/

+2  A: 

Are you using any other JavaScript libraries? If so, you will probably need to use jQuery in compatibility mode:

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

tambler
+2  A: 

I prefer using firebug or similar tool to check if the jquery.js is being downloaded or not. Check if there are any 404's.

Teja Kantamneni
+1  A: 

I use Url.Content and never have a problem.

<script src="<%= Url.Content ("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script>
mdm20
+4  A: 

That error can only be caused be one of three things:

  1. Your JavaScript file is not being properly loaded into your page
  2. You have a botched version of jQuery. This could happen because someone edited the core file, or a plugin may have overwritten the $ variable.
  3. You have JavaScript running before the page is fully loaded, and as such, before jQuery is fully loaded.

You should check the Firebug net panel to see if the file is actually being loaded properly. If not, it will be highlighted red and will say "404" beside it. If the file is loading properly, that means that the issue is number 2.

Make sure all javascript code is being run inside a code block such as:

$(document).ready(function () {
  //your code here
});

This will ensure that your code is being loaded after jQuery has been initialized.

One final thing to check is to make sure that you are not loading any plugins before you load jQuery. Plugins extend the "$" object, so if you load a plugin before loading jQuery core, then you'll get the error you described.

Mike Trpcic
"That error can only be caused be one of two things"... That is three things.. :P
CodeKiwi
Ahh, caught me. Changing it now.
Mike Trpcic
A: 

Post a link tot he actual page... fastest way.

jeffkee
intranet only im afraid.
Paul Creasey
A: 

I got the same error message when I misspelled the jQuery reference and instead of type="text/javascript" I typed "...javascirpt". ;)

alan