Skip to content

Instantly share code, notes, and snippets.

@ipartola
Created February 9, 2016 16:52
Show Gist options
  • Save ipartola/f6547247e4dd8fcf0190 to your computer and use it in GitHub Desktop.
Save ipartola/f6547247e4dd8fcf0190 to your computer and use it in GitHub Desktop.
JavaScript and jQuery notes

JavaScript, the DOM, and jQuery

This talk will cover JavaScript, the DOM, and jQuery as three separate subjects. Why? Because they are all interrelated.

Part 1: JavaScript

  • JavaScript goes by many names. The original was Mocha and LiveScript. The more politically correct modern name is ECMAScript, but that sounds like a skin disease, so we don't use it.
  • Developed in 10 days in 1995 by Brendan Eich and Netscape.
  • JavaScript is one of the most popular programming languages in the world. Learn it. JavaScript developers are in demand, and that's likely to continue.
  • JavaScript runs in the browser, as well as on the server. Node.js is a popular way to run JavaScript on the server.
  • JavaScript's goal in the browser is to enhance the web pages and/or deliver complete client-side applications. We will focus on the former.

Note: to open the Developer console:

  • Firefox: Ctrl + Shift + K or Command + Option + K
  • Chrome: Ctrl + Shift + J or Command + Option + J

Some examples of JavaScript in action:

For further instruction try:

Part 2: The DOM

The Document Object Model, a.k.a. the DOM is a model for representing and interacting with HTML and XML-like documents. The DOM represents the elements of a document as "nodes", which are organized in a tree structure: aka DOM tree. The DOM provides a way to examine and manipulate the nodes: search them, read and write their properties, create nodes, remove nodes, move nodes within the tree, and respond to external changes to the DOM via "events":

What external changes can happen to the DOM? Two sources: JavaScript code (that you or someone else controls), and user interactions. Examples:

  • Showing or hiding a piece of text on a timer.
  • User clicking a button.
  • Special case: AJAX events.

Theoretically, it is possible to use the DOM from JavaScript directly. Indeed this is what happens under the hood of all JavaScript libraries (such as jQuery). So why don't we do that?

  • DOM implementations are inconsistent between browsers. Some are intentional, and some are just bugs fixed in the next version.
  • DOM API (methods for manipulating the DOM) are old, verbose, and not helpful for a lot of common tasks.
  • DOM API lacks certain basic features that everyone needs.
  • DOM events API is a mess.

Examples:

Part 3: jQuery

jQuery is a mature library that smoothes out interactions with the DOM. It provides a unified interface for everything the DOM offers, and does it through a pleasant API. It also provides fixes for all manner of browser bugs under the hood, so you don't have to do it ever know they existed. jQuery is functional: you can chain different manipulations of the DOM together:

jQuery also includes a wrapper around AJAX methods, as well as a number of useful utility functions that plain JavaScript does not yet include. Examples of what jQuery can do:

jQuery is extensible. Anyone can create "plugins" for it by creating their own DOM manipulation functions and telling jQuery about them. There are lots of plugins available, of varying quality. There is also jQuery.UI which is a UI framework based on jQuery. It is generally not necessary for most web sites, but may be useful for web applications.

Further resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment