jQuery is JavaScript.
In other words, jQuery is a library written / constructed with the JavaScript language.
There are two main reasons for having a library like jQuery:
- Can simplify life to the programmer, with fewer lines of code to do things than in native JavaScript would need more lines
- have an API that is the same in all browsers that this library supports
Thinking about the jQuery library as a tool to make life easier for the programmer, the idea is to save work, in the extension of code needed to write and minimize possible errors.
For example an ajax call to fetch a JSON would look like this in jQuery:
$.getJSON('/o/meu/url', function(data) {
});
To do the same in native JavaScript you would need more rows:
var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// tudo correu bem!
var data = JSON.parse(request.responseText);
// aqui é o mesmo que o interior do exemplo com jQuery
} else {
// este pedaço é corrido se houver algum erro
}
};
request.onerror = function() {
// este pedaço é corrido se houver algum erro
};
request.send();
In practice jQuery has these lines all written in your library (in the JavaScript file that we have to load with the page) but it shows the programmer only the methods that build and simplify things as I showed in the first example.
jQuery is not always necessary, in fact it is not as important as it once was. An interesting site with common features that do well in native JavaScript nowadays: link
When we program an application, and users use our program in different browsers, we run the risk that the JavaScript we write will not work in some browsers. This is because each one implements things with slight differences, or because some are more modern than others. The role of libraries such as jQuery is to create an abstraction layer to only use its API, not native JavaScript. Thus, within the library, the necessary corrections and adjustments are made to a jQuery code as $(document).on('click', ...
to be decomposed into JavaScript that a given browser understands and provide the same functionality.
A classic example is the way to tie event headphones in older versions of Internet Explorer.
The modern way is elemento.addEventListener
but IE was elemento.attachEvent
. To avoid writing a date code like this ( source ):
//addEventListener polyfill 1.0 / Eirik Backer / MIT Licence
(function(win, doc){
if(win.addEventListener)return; //No need to polyfill
function docHijack(p){var old = doc[p];doc[p] = function(v){return addListen(old(v))}}
function addEvent(on, fn, self){
return (self = this).attachEvent('on' + on, function(e){
var e = e || win.event;
e.preventDefault = e.preventDefault || function(){e.returnValue = false}
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true}
fn.call(self, e);
});
}
function addListen(obj, i){
if(i = obj.length)while(i--)obj[i].addEventListener = addEvent;
else obj.addEventListener = addEvent;
return obj;
}
addListen([doc, win]);
if('Element' in win)win.Element.prototype.addEventListener = addEvent; //IE8
else{ //IE < 8
doc.attachEvent('onreadystatechange', function(){addListen(doc.all)}); //Make sure we also init at domReady
docHijack('getElementsByTagName');
docHijack('getElementById');
docHijack('createElement');
addListen(doc.all);
}
})(window, document);
We use jQuery, which has more or less this inside your file .js
and we use its abstraction layer (API) that is only:
$(elemento).on('nome-do-evento', ...
More information: