views:

25

answers:

1

I am including jQuery in the head of a personal site. I also included an external js file right after it that uses jQuery.

<script language="javascript" src="../js/jquery-1.4.2.min.js"></script>
<script language="javascript" src="../js/global.js"></script>

global.js merely contains the following:

$(".nav-item").hover(function(){
    $(this).addClass("selected");
},
function(){
    $(this).removeClass("selected");
});

When I tried it out, I was noticing that my error console was giving me a "$ is not defined error". After moving my external js file to the bottom of the page, everything works fine. What is the proper way to deal with what is apparently a timing issue?

Thanks in advance for any help

EDIT: It seems that while I did see the undefined error last night, I must have done something to get rid of it and am not able to see or reproduce it anymore. I tried binding the events to document.ready, and that seems to solve my problem.

+1  A: 

Did you bind the event handler in global.js to document ready?

meder
That's a completely different problem, his issue is $ not being defined. $(document).ready() would still throw "$ is not defined".
Marko
I realize that and it doesn't make sense to me, but due to lack of information this is what I'd advise until he posts more.
meder
This fixed my issue (see Edit). Thanks for the help!
TeeOh