window,navigator- BOM - Browser Object Model
navigatorrepresents the the browser basic code (or kernel) and properties, like version, user agent string, language, geolocation options and so on.windowis a "god object" containing all browser components, such as the address bar (location), modal dialogues (alert(),prompt()) and the window being displayed on screen itself. It's also the top object where all global variables are attached to.
- BOM - Browser Object Model
document- DOM - Document Object Model
- Contains all of the content being displayed - in other words, whatever is inside the
window. - The
documentrepresents all content as nodes - tags, text, images, and so on. - The document API makes these nodes available to the Javascript engine
document.getElementById- find a single element using itsiddocument.getElementsByClassName/document.getElementsByTagName- find all elements by class or tagdocument.querySelector- find a single element using a jQuery-like selectordocument.querySelectorAll- find all elements matching a selector
- Events
element.addEventListener- attaches a function to an event. Examples:button.addEventListener('click', function(ev){...})- run function when button is clickeddocument.addEventListener('mousemove', function(ev){...})- run function when the mouse is moved
DOMContentLoaded- A useful event that's triggered when the page finishes loading
- Usage:
document.addEventListener('DOMContentLoaded', function(){...}) - If you're using jQuery, you can use the
$(function(){...})shortcut instead
- Propagation
- Events always propagate outwards from the innermost object - the one that's nested deeper in the document.
- This means that if you click a
buttoninside adivthat's inside anotherdiv, all three elements will receive theclickevent. - To prevent propagation, use
event.stopPropagation()
- Javascript code is asynchronous and event-driven. This means it will likely run out of order.
- Because of that, it's really important to organize your code in short, simple functions.
- This is especially useful when dealing with callbacks - and all events will require one.
- About DOM objects:
- If an object can contain text or html, you can get and set its contents by using the
innerTextandinnerHTMLproperties. - You can also get/set element attributes by using the
getAttribute()/setAttribute()methods.
- If an object can contain text or html, you can get and set its contents by using the