tags:

views:

34

answers:

3

why does some javascripts come in conflict with some other ones? I mean I had been using javascript code for image gallery and then tried to get the text watermark in jquery. Why is that after using the jquery, the gallery totally disappeared? there was no common ids that I used inthe two scripts. Any reason for that?

+1  A: 

Try this one: http://api.jquery.com/jQuery.noConflict/

Mathias Lin
that really has nothing to do with what he asked.
nickf
sure it does. If obviously jquery is the issue here, then jquery probably overrides functions or variables, such as $ or something else, which is quite common.
Mathias Lin
apologies .. but no idea whats there in this??? getting too confused....
Sachindra
that's only if a different library is included. `noConflict` just restores `$` to be its previous value before jQuery was included. As long as the OP isn't mixing up jQuery and Prototype (for example), then that's not the problem.
nickf
what you comment above is exactly the point. if the gallery he uses is part of a library or other js framework, or if his gallery overrides the $ itself (and we don't know his gallery code) which I just assumed, then noConflict can be the right solution.What I described with $ is exactly the same issue of what you just described with your 'textTitle' variable.
Mathias Lin
+1  A: 

Sometimes people may use global variables in their packaged javascript. If you have two packages which use the same global variable, obviously there's going to be problems. To resolve it, what you could try is to create a closure around each different package's code. Without seeing the code, I'm not 100% that it would work, but here's an example:

// gallery.js
var textTitle = "Image gallery";

function getGalleryTitle() { return textTitle; }


//////////////
// watermark.js

var textTitle = "Watermark";


alert(getGalleryTitle()); // "Watermark"  :(

Now with closures, created by using anonymous functions:

(function() {
    var textTitle = "Image gallery";
    function getGalleryTitle() {
        return textTitle;    // this will always be "Image gallery"
    }
})();

(function() {
    var textTitle = "Watermark";    // won't conflict with any other code.
})();

The downside here is that you can no longer access those variables and functions globally (eg, via inline event handlers).

nickf
+2  A: 

As Mathias has correctly pointed out, the most likely problem is that your other library is also using the $ symbol -- when you added jQuery to the page it overwrote the old $ variable with its own $ ... and your first javascript library ceased to work. The solution is to call jQuery.noConflict() to restore the $ variable to the first library. You will still be able to use jQuery plugins -- you will just need to update example scripts that use $ to use jQuery instead. Thus $("#my_content").css({color: "red"}); would become jQuery("#my_content").css({color: "red"});

Alternately, you can assign the jQuery object to another variable object in this manner:

$jq=jQuery.noConflict(); //From now on jQuery can be called with the $jq function

or you can use it within a closure:

(function($) {
    // $ in here will map to jQuery
    //outside of this code $ will map to your other library's $
})(jQuery.noConflict());
Sean Vieira
thanks for making it simple...
Sachindra