views:

101

answers:

4

in my job i always have to use jquery with prototype can i make any custom jquery with library file like once i will add in head and then no need to change anything in any code.

is it possible?

I don't want to change anything in an code $ to jQuery.

+3  A: 

This just begs the question why dont you simply implement all your stuff in Prototype then - or implement everything in jQuery. Overall they both have the same capabilities.

However to directly answer your question - ther isnt really a way to make a custom build of jQuery. But you could simply put your noConflict call immediately following your inclusion of the jQuery library.

Then for your stuff you can simply wrap it all in

(function($){
  // your jQuery code here... Can be used with $ as normal.
})(jQuery);

within that function youll be able to use $ as the alias - outside of it you would need to use jQuery. The only gotcha here is that if you want a variable defined inside here to be global youll need to make it that way manually as everything within this will be scoped to the function. You can do this like so:

(function($){
  window.myGlobalVar = 'This is a global variable';
})(jQuery);
prodigitalson
+3  A: 

from http://www.cssnewbie.com/runing-jquery-with-other-frameworks-via-noconflict/

Keeping it Short

The noConflict mode does have one other bit of functionality that I’ve found useful in some of my projects: you can select a different variable to use instead of the standard “jQuery”. The usage looks like this:

var $j = jQuery.noConflict();

Now in addition to using the default jQuery() notation, I can also use the shorter $j() notation. This allows me to avoid running into problems with other frameworks, while still enjoying almost the same conciseness in my code.

Josh
This way definitely works as well however you would need to change all your calls to `$` to `$j`.
prodigitalson
This is the way I do everything, but it's obviously easiest if you do this right from the start. IMO, its the best way to go as you're also future-proofing against any other libraries you might include in the future which could use `$`.
Graza
A: 

The best thing is often to wrap the $ in a private scope:

(function($) {
    // jQuery stuff
})(jQuery)

You can also call the jQuery.noConflict() function before the wrap: http://api.jquery.com/jQuery.noConflict/

Another clean option is chaining the noConflict() into a domReady callback:

jQuery.noConflict()(function($){
    // code using jQuery or $
});

console.log(typeof $ === 'undefined') // prints true
David
With your second option do you need to pass in `jQuery` as the argument `$` in order to use the $ alias within the anon function? (Never seen it done this way before...)
prodigitalson
You are right, if you want to use the $ inside, you should pass it as an argument. Corrected.
David
A: 

To address your question of whether you can have an "include-once" file, yes, you could do something similar to:

/* MyLibLoader.js */
document.write("<script type='text/javascript' src='prototype.js'></script>");
document.write("<script type='text/javascript' src='jquery.js'></script>");
window.$j = jQuery.noConflict();

And then in HTML

<head>
  <script type='text/javascript' src='MyLibLoader.js'></script>
</head>

But this approach assumes you're happy to change your jQuery usage to $j

Graza