Does web design need education?
The profession of web designer is now becoming one of the most sought after. Every month, thousands of new people with burning eyes appear who think that this is their…

Continue reading →

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…

Continue reading →

Changing the site design: quick restart of your project
I wrote many times that the web design master must be creative, must look for new solutions to everyday situations, keep an eye on fashion ... But unfortunately, fashion is…

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.

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…

...

Web Design Fundamentals
Often, inexperienced developers do not adhere to generally accepted standards, either because of ignorance, or because of the idea that they are smarter than others, and therefore get a terrible…

...

Web design standards: everything you need to know for beginners and pros
How do design standards increase website popularity? There is an opinion that web design standards attract users to sites, and most importantly - keep them for a long time. Why?…

...

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…

...