0

I am already declaring jQuery at the top of my <head>, below that I am referencing a ton of other plugins. When I do this in the body of my page:

<script type="text/javascript">
    $(function() {
        alert('hello');
    });
</script>

I don't get the alert. However if I redeclare jQuery again like so:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">
    $(function() {
        alert('hello'); 
    });
</script>

It works. Is there any way to reassign the $ back to jQuery (which is what I'm guessing is the problem here). So I can use $ instead of jQuery noConlict.

2
  • 2
    you do of course know that you could just assign jQuery to some other variable ie. $j = jQuery.noConflict(); and wouldn't have to worry about handing over control back and forth. Commented Sep 16, 2011 at 20:31
  • i dont understand why you would want to hand it back if you can permanently solve your issue and not have to keep track of who has control. Commented Sep 16, 2011 at 20:34

4 Answers 4

1
<script type="text/javascript">
   jQuery(function($) {

      alert('hello');
      //use $ however u want

   });
</script>

Fiddle (using $_): http://jsfiddle.net/maniator/B3hU4/

Sign up to request clarification or add additional context in comments.

Comments

1

You are placing your jQuery inline on the page rather than in a document ready block. If you don't have them in a document ready block, there's no guarantee that jQuery has fully loaded before the function is fired.

Comments

1

Encapsulate like this:

<script type="text/javascript">
   (function($) {
      alert('hello');
   })(jQuery);
</script>

Comments

1

You can also just do this if you want $ returned to be jQuery globally:

window.$ = jQuery;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.