views:

7622

answers:

8

Hi,

Several of my pages use both JQuery and Protoype. Since I upgraded to version 1.3 of JQuery this appears to be causing problems, because both libraries define a function named '$'.

JQuery provides a function noConflict() which relinquishes control of $ to other libraries that may be using it. So it seems like I need to go through all my pages that look like this:

<head>   
    <script type="text/javascript" src="/obp/js/prototype.js"></script>
    <script type="text/javascript" src="/obp/js/jquery.js"></script>
</head>

and change them to look like this:

<head>   
    <script type="text/javascript" src="/obp/js/prototype.js"></script>
    <script type="text/javascript" src="/obp/js/jquery.js"></script>
    <script type="text/javascript">
     jQuery.noConflict();
     var $j = jQuery;
    </script>
</head>

I should then be able to use '$' for Prototype and '$j' (or 'jQuery') for JQuery. I'm not entirely happy about duplicating these 2 lines of code in every relevant page, and expect that at some point somebody is likely to forget to add them to a new page. I'd prefer to be able to do the following

  • Create a file jquery-noconflict.js which "includes" jquery.js and the 2 lines of code shown above
  • Import jquery-noconflict.js (instead of jquery.js) in all my JSP/HTML pages

However, I'm not sure if it's possible to include one JS file in another, in the manner I've described? Of course an alternate solution is simply to add the 2 lines of code above to jquery.js directly, but if I do that I'll need to remember to do it every time I upgrade JQuery.

Thanks in advance, Don

+4  A: 

It would seem that the most simple answer would be to bite the bullet, and include your noConflict lines. Of course if your pages aren't using a shared header, that solution might not be the best.

Jonathan Sampson
+2  A: 
document.write(unescape('%3Cscript type="text/javascript" src="/obp/js/jquery.js"%3E%3C/script%3C');
jQuery.noConflict();
var $j = jQuery;

or

var scripty = document.createElement('script');
scripty.href="/obp/js/jquery.js";
document.getElementsByTagName('head')[0].appendChild(scripty);
jQuery.noConflict();
var $j = jQuery;

EDIT:

I tried out this suggestion but the last 2 lines produce the error

jQuery is not defined
jacobangel
Good thinking ;)
Jonathan Sampson
You need to specify a callback when the script loads to run the noconflict code... just do a .appendChild(scripty)scripty.onload = setNoconflict;function setNoconflict(){ jQuery.noConflict(); $j = jQuery; }
CodeJoust
A: 

I went through this for a while. It is very annoying and in the end I decided to weed out all of my old prototype stuff and replace it with jquery. I do like the way prototype handles some Ajax tasks but it wasnt worth the trade off of maintaining all of the no conflict stuff.

Nick
A: 

Don,

If I were you, I'd drop my no conflict code into a javascript include file like you opined about above, and then figure out some process where I'd be setting these things I need to include in all my pages in one central place. If you are working with straight HTML files and you don't have any kind of templating/scripting capability server-side for what gets included in a document, there's always the possibility of doing a Server-Side Include.

Either way, the pain you'll experience updating each of your pages this time will come back again when you need to update your analytics code or the site footer.

Cheers, Billy

Billy Gray
A: 

You could call jquery first and then set

var $j = jQuery;

prototype will take control of $ in this case.

Or, you could just refer to jquery by using the full jquery function name (jQuery).

RobKohr
+2  A: 

Could you not just include the jQuery = noConflict() code in the jquery.js source file? Why would it need to be defined that way?

Chris
A: 

use prototype below the jQuery .. like below . it will work

but in this case jquery function will create some problem . so u can use

jQuery.noConflict(); var JQ = jQuery;//rename $ function

to solve the problem .

Curzon Rahman
A: 

this solution worked fine.

jQuery.noConflict();

var $j = jQuery;

Now use $j in place of $ for your jquery code like

$j(document).ready(function() {

$j('#div_id').innerfade({ 

     // some stuff

});

});

AJCE