Creative website design: what's new?
Where does creative start? Morning, coffee, shower ... A new day has begun, it is time to create. We open the laptop or start the computer, before us is a…

Continue reading →

Landing design: how to make a capture page
What is a landing page? The concept of landing page, as, in principle, the landing design, came to us relatively recently, according to the official version from the USA, 10-12…

Continue reading →

How to become a web designer, and what qualities should a really cool specialist have?
From the author: many of you were probably looking for the answer to the question: how to become a web designer? Of course, this is a difficult question, to which…

Continue reading →

What is $ .noConflict ()?

jQuery uses the $ character as a shorthand for jQuery. Many other libraries also use this symbol. So, connecting several of these libraries may lead to the fact that some of them will not work.

Since jQuery $ is just an abbreviation for a jQuery object, the use of the $ character is optional. To prevent conflicts, jQuery provides the noConflict () method. Let’s see how the noConflict () method works.

$ .noConflict ()
When initialized, jQuery remembers the current values ​​of the global variables $ and jQuery in local variables. The $ .noConflict () method simply restores the values ​​of global variables from local ones. In doing so, it takes one parameter that indicates whether to restore the jQuery variable or not.

How it works? Let’s say we connected some library using the variable $, and after it – jQuery.

Before initializing its own global variable $, jQuery remembers its value in a local variable:

Javascript
var _ $ = window. $;
When calling noConflict, if the jQuery object is contained in the $ variable, the previous value from the _ $ variable is restored.

Javascript
if (window. $ === jQuery) {
window. $ = _ $;
}
The noConflict method accepts the deep parameter, if it is passed, the jQuery variable will also be restored:

Javascript
if (deep && window.jQuery === jQuery) {
window.jQuery = _jQuery;
}
This can be useful if you fear that jQuery has already been connected.

Work with jQuery with noConflict
How to work with jQuery without $? First, you can use the jQuery variable:

Javascript
$ .noConflict ();
jQuery (document) .ready (function () {
jQuery (“button”). click (function () {
jQuery (“# ​​result”). text (“Button clicked!”);
});
});
Secondly, if you still want to use $, you can wrap the script in an immediately called function with $ parameters, the value of which will be a jQuery object.

Javascript
(function ($) {
$ .noConflict ();
$ (function () {
$ (“button”). click (function () {
$ (“# result”). text (“Button clicked!”);
});
});
}) (jQuery);
In the example above, $ is a local variable in the immediately called function.

The callback function that is passed to jQuery (document) .ready () takes one parameter – a reference to the jQuery object. The third method follows from this – working with the local variable $ inside this handler:

Javascript
$ .noConflict ();
jQuery (document) .ready (function ($) {
$ (“button”). click (function () {
$ (“# result”). text (“Button clicked!”);
});
});
Finally, the fourth way is to define your own variable, which will be a reference to a jQuery object:

Javascript
var j = $ .noConflict ();
j (document) .ready (function () {
j (“button”). click (function () {
j (“# result”). text (“Button clicked!”);
});
});
NoConflict implementation for native modules
If you are developing your own javascript library, you can use global variables with non-unique names, which, in turn, can lead to conflicts with other code. It’s good practice to have a noConflict method that, like jQuery, would restore the previous value of a global variable. Consider the implementation of such a function.

Javascript
(function (name, definition) {
var module = definition (),
global = this,
old = global [name];
module.noConflict = function () {
global [name] = old;
return module;
};
global [name] = module;
}) (‘PdfExporter’, function () {
PdfExporter = (function () {
return {
export: function () {/ * … * /}
};
}) ();

return PdfExporter;
});
In this example, we describe the module as an immediately called function that takes two parameters: the name of the module and its definition. The example name is ’PdfExporter’, and the definition is a function that returns a PdfExporter object.

When executed, this function remembers the PdfExporter object in the local variable module, and the current value of the PdfExporter variable in the local variable old. Next, the noConflict function is added to the module object and the module value is written to the global variable PdfExporter. The noConflict function restores the value of the PdfExporter variable from the old variable.

You can use our library in the same way as described for jQuery, for example:

Javascript
var myExporter = PdfExporter.noConflict ();
myExporter.export ();
An immediately called function can also initialize a module in the style of AMD or CommonJS, we will talk about this in more detail separately.

Conclusion
We examined what the $ .noConflict method is, how it works and what it is for. Proper use of noConflict will allow us to avoid not only the conflict of the $ variable with other libraries, but also the conflict of jQuery versions among themselves. It’s also good practice to add the noConflict function to your own libraries to avoid name conflicts.

Website design in Photoshope: getting to know the editor and its features
Photoshop and web design 98% of web design projects begin with the creation of functional layouts for future sites, and almost all of them are now drawn in Photoshop. What…

...

Website design in Photoshope: getting to know the editor and its features
Photoshop and web design 98% of web design projects begin with the creation of functional layouts for future sites, and almost all of them are now drawn in Photoshop. What…

...

Website design in Photoshope: getting to know the editor and its features
Photoshop and web design 98% of web design projects begin with the creation of functional layouts for future sites, and almost all of them are now drawn in Photoshop. What…

...

Visual Regression Testing with PhantomCSS
If you worked on large, serious projects, you probably already felt the importance of automated testing during the development process. Depending on experience, this awareness may come suddenly like a…

...