3

I added JQuery to my HTML file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 

Then I add a link to my JavaScript file:

<script src="public/javascripts/new_javascript.js" type="text/javascript"></script> 

(I checked that this link work.)

In the file I do

$(document).ready(function() {
   alert("hey!");
});

But the Google Chrome developer tool shows an error:

Uncaught TypeError: Object #<HTMLDocument> has no method 'ready'

How can it be an error?

I'm doing this in Rails, and the HTML is like:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>   
<script src="public/javascripts/prototype.js?1349898477" type="text/javascript"></script> 
<script src="public/javascripts/effects.js?1349898477" type="text/javascript"></script> 
<script src="public/javascripts/dragdrop.js?1349898477" type="text/javascript"></script> 
<script src="public/javascripts/controls.js?1349898477" type="text/javascript"></script> 
<script src="public/javascripts/rails.js?1349898477" type="text/javascript"></script> 
<script src="public/javascripts/application.js?1349898477" type="text/javascript"></script> 
<script src="public/javascripts/new_javascript.js?1351137775" type="text/javascript"></script> 
8
  • Do you have any other script tags on your page? Commented Oct 25, 2012 at 4:11
  • 2
    This error is specific to jQuery not being loaded. Can you confirm jQuery is being included in your HTML BEFORE your script file? Can you give us an example of your HTML? Commented Oct 25, 2012 at 4:11
  • 2
    Are you also using Prototype? Commented Oct 25, 2012 at 4:11
  • 1
    Prototype is overwriting the $ function used by jQuery. Why do you have them both? Commented Oct 25, 2012 at 4:16
  • Try jQuery instead of $ (because Prototype has overwritten $). Commented Oct 25, 2012 at 4:17

3 Answers 3

5

jQuery and Prototype conflict with each other. Try using jQuery instead of $ for jquery commands:

jQuery(document).ready(function() {
   alert("hey!");
});
Sign up to request clarification or add additional context in comments.

1 Comment

0

If prototype still overwrites $ like it's the dark ages, wrap your JQ code in a function like so:

(function($) {

    // Do whatever you like with $

    $(function() {
        alert("Document ready");
    })

})(jQuery);

Comments

0

You can use jQuery.noConflict()

$.noConflict();
jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
});
// Code that uses other library's $ can follow here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.