views:

576

answers:

4

I use

< script type="text/javascript" src="jquery-1.2.2.pack.js"> < /script>

to load jquery and then load an external script that contains these :


var jkpanel={ controltext: 'menu', $mainpanel: null, contentdivheight: 0,

openclose:function($, speed){
 this.$mainpanel.stop() //stop any animation
 if (this.$mainpanel.attr('openstate')=='closed')
  this.$mainpanel.animate({top: 0}, speed).attr({openstate: 'open'})
 else
  this.$mainpanel.animate({top: -this.contentdivheight+'px'}, speed).attr({openstate: 'closed'})
},

init:function(file, height, speed){
 jQuery(document).ready(function($){
  jkpanel.$mainpanel=$('<div id="dropdownpanel"><div class="contentdiv"></div><div class="control">'+jkpanel.controltext+'</div></div>').prependTo('body')
  var $contentdiv=jkpanel.$mainpanel.find('.contentdiv')
  var $controldiv=jkpanel.$mainpanel.find('.control').css({cursor: 'wait'})
  $contentdiv.load(file, '', function($){
    var heightattr=isNaN(parseInt(height))? 'auto' : parseInt(height)+'px'
    $contentdiv.css({height: heightattr})
    jkpanel.contentdivheight=parseInt($contentdiv.get(0).offsetHeight)
    jkpanel.$mainpanel.css({top:-jkpanel.contentdivheight+'px', visibility:'visible'}).attr('openstate', 'closed')
    $controldiv.css({cursor:'hand', cursor:'pointer'})
  })
  jkpanel.$mainpanel.click(function(){jkpanel.openclose($, speed)})  
 })
}

}

//Initialize script: jkpanel.init('path_to_content_file', 'height of content DIV in px', animation_duration) jkpanel.init('1', '80px', 1000)


and also use a mootools plugin of course.

MY QUESTION IS THAT how should I use var $j = jQuery.noConflict(); in the above script to prevent conflicting

+3  A: 

Wrap all the JavaScript that relies on jQuery in a closure to prevent namespace conflicts, like so:

// Start closure to prevent namespace conflicts
;(function($) {

  // Whatever code you want that relies on $ as the jQuery object

// End closure
})(jQuery);

It looks weird, but the syntax is right (yes, the first line starts with a semicolon). This automatically substitutes jQuery for the $ object, which both jQuery and mootools make use of. Since you're using both, you should wrap all of your jQuery code in a closure like this (one for each .js file or script tag).

No Surprises
Why does the first line start with a semicolon? Also: as far as I can tell, all of the jQuery code in the question *is* wrapped in code like this: it's all in the ready callback, which gets (and uses) jQuery as its parameter.
Sixten Otto
I've been confused and code does not work. can you please give me final code with the fix from the code I insert above?
Ali
btw, you can do exactly the same for mootools 1.2.3+, just replace last })(jQuery) with })(document.id)
Mike Korobov
what is document.id dear Mike? I'm so new to jquery , please integrate my script above with the noConflict(); function and give me final code
Ali
A: 

If the problem is just, you load MooTools, and then you load jQuery, and then MooTools doesn't work because jQuery has taken over the dollar function, then you probably just need code like this:

<script type="text/javascript" src="mootools.js"> </script>
<script type="text/javascript" src="jquery-1.2.2.pack.js"> </script>
<script type="text/javascript">
  jQuery.noConflict();
</script>

That should get jQuery to relinquish $(). The code you have in your question already does the other handy thing, which is use the parameter to the ready event handler as a way to locally use a shorter name for the jQuery object.

I'd strongly recommend reading the jQuery page on working with other libraries and maybe the documentation for the noConflict() function.

Sixten Otto
A: 

Thank you all,

I read those but I don't know how to use it on my specific script , could you integrate my script above with noConflict() function and give me final code please?

Regards...

Ali
Stack Overflow is not about letting others do your work. Look at the great answers you're given, learn from it and try to do it yourself. Then, when another question arises, feel free to post it here and learn even more.
Ronald
A: 

noconflict() will do magic in JQuery. http://praveenbattula.blogspot.com/2010/02/jquery-conflict-when-using-different.html

Rare Solutions