Layout adaptive menu
Adaptive menus collapsing into a hamburger on mobile devices can now be seen on almost any site. In this lesson we will learn to make up such menus. In the…

Continue reading →

Basic web design elements. How to create an effective website thanks to them?
For the construction of any building requires certain building materials. As a rule, for each type of structure they constantly remain the same, only the shape and general design of…

Continue reading →

Button State Design
From the author: button design is the main element of any design system. The best of the buttons are simple, versatile, perhaps even a little fun. But there are many…

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.

Usability in web design, or how to evaluate your site from the point of view of the user
From the author: if you are visiting a site for the first time, then this event can be compared to traveling in an unfamiliar area: the local people, of course,…

...

Basic web design elements. How to create an effective website thanks to them?
For the construction of any building requires certain building materials. As a rule, for each type of structure they constantly remain the same, only the shape and general design of…

...

Best One-Page Website Designs (Landing Page)
An illiterate landing page to which new users are directed can lead to the depreciation of the entire advertising company. If you want to build a successful and carefree career…

...

Working with empty space: why emptiness affects design so much
in web design, empty spaces include areas without text and images. It can be said that this is “visual silence”. For our design to function, it is necessary to correctly…

...