Presented at JoomlaDay Chicago 2019
Joomla 4 is on the horizon and will be closer than ever to the standards of the modern web. Underlying scripts will not rely on external libraries any longer, reducing dependencies for the years ahead. For small javascript tasks, writing directly to the DOM may be faster than relying on libraries such as jQuery.
In this hands-on session, we will see how small jQuery tasks can be easily rewritten with plain javascript.
4. Olivier Buisard
JQUERY ADVANTAGES AND
DRAWBACKS
• Written in JavaScript
• Supports old browsers
• Code simplification, chaining
• Many available plugins
• Extra library file including features you may not need
• Possible conflicts with other libraries
• Heavy to operate on mobile devices
• Overuse and inefficiency
10/11/2019
5. Olivier Buisard
ADVANTAGES IN LEARNING TODAY’S
PURE JAVASCRIPT
• Fast (native to the browsers)
• Clean, efficient code
• Helps understand jQuery underlying code
10/11/2019
6. Olivier Buisard
SEPARATION AND DELEGATION
• HTML:
the structure (content, buttons, links)
• CSS:
the visual, the interactivity, the layout
• JavaScript:
the loading of content on demand (AJAX), the event listeners
(for touch, clicks and scroll…)
10/11/2019
8. Olivier Buisard
SELECTING ELEMENTS – ‘$’
10/11/2019
$(".tile");
$("#mytile");
// select the first instance of .tile
document.querySelector(".tile");
// select all instances of .tile
document.querySelectorAll(".tile");
// select ‘the’ instance of #mytile
document.getElementById("mytile");
9. Olivier Buisard
USING SELECTED ELEMENTS
10/11/2019
$(".tile").hide();
document.querySelectorAll(".tile").forEach(function(tile) {
tile.style.display = "none";
});
10. Olivier Buisard
TRAVERSING THE TREE
10/11/2019
var tile = $(".tile");
tile.next();
tile.prev();
tile.parent();
var tile = document.querySelector(".tile");
tile.nextElementSibling;
tile.previousElementSibling;
tile.parentElement;
11. Olivier Buisard
WORKING WITH THE DOM
10/11/2019
// Create a div and add text
var element = $("<div/>");
element.text("This is just text");
// Create a div and add text
var element = document.createElement("div");
var text = document.createTextNode("This is just text");
element.appendChild(text);
16. Olivier Buisard 010/11/2019
Make sure that no one will be blocked
by using an older browser
if they don’t get all interactions
that are meant for a new technology
20. Olivier Buisard
YOUR OWN POLYFILL
10/11/2019
if (!Array.prototype.push) {
// Check if not already supported, then only add.
// No need to check this when you want to Override the method
// Add method to prototype of array,
// so that it can be directly called on array
Array.prototype.push = function() {
// Use loop for multiple/any no. of elements
for (var i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
}
// Return new length of the array
return this.length;
};
}
21. Olivier Buisard
SHOULD I GET RID OF JQUERY?
• In Joomla 3, if the template uses Bootstrap 2, 3 or 4,
jQuery is included
• For complex interactions (ie slider), use jQuery
• For simpler code, go with pure JavaScript
• Joomla 4 is slowly becoming jQuery independent
• Bootstrap 5 will not require jQuery
10/11/2019