views:

355

answers:

2

I have the following code in my page. Sometimes the jquery is loading fine but sometimes i get an error: jQuery is undefined

<script src="/javascripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/javascripts/jquery-ui-1.7.1.custom.min.js"    
type="text/javascript"></script>

<script type="text/javascript">
alert('test')
var $j = jQuery.noConflict();
alert('test1')
//start when document is ready
$j(document).ready(function(){
alert('test2')

When jquery does not load I only see first alert on the page. So I am thinking jQuery is never loaded. Also I am using the noConflict just because before I was gettin error $ is undefined. I am not using any other js library like prototype or anything. So I dont have any REAL need to use jQuery noConflict()

Has someone come across these kind of problems with jqueyr and IE 7? This error is not happening on Firefox.

Can something be done to fix this?

Update I decided to make use of jquery + jquery ui hosted on google. which worked for me.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>
A: 

If this happens in other browsers as well, then my first bet is your URL paths to the Jquery libraries are incorrect. (javascripts or javascript). Try putting the full URL to the library and see what happens.

Is the noConflict declaration correct? I think it should be

var j = jQuery.noConflict();
....
j(document).ready(function(){....}
Ahmad
declaration is correct. I took it from jquery's site. http://docs.jquery.com/Using_jQuery_with_Other_Libraries
Omnipresent
correct..my mistake
Ahmad
+1  A: 

If you don't have any other conflicting libraries, then $ is undefined pretty much means either your script library path for jquery is incorrect, or that jQuery isn't ready yet. There's no reason in this case to use the no conflict functionality.

Try this:

<script src="/path to your/jquery-1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {

        alert("jquery is ready.");

    });
</script>

If the alert fires, you know jQuery is referenced and initialized properly.

If not, try these options:

  • Try using Firefox with Firebug to check that the script path is correct (you'll be able to actually load up the script file in the firebug pane)
  • Use the non-minified version of jQuery in development, to ensure there are no issues there
KP