The DOM vs the BOM
Document Object Model
: Also known as the DOM
contains a collection of nodes (HTML Elements), that can be access and manipulated.
document
object is a Web Page, and the DOM is the hierarchy of browser objects.Browser Object Model
: Also known as the BOM
, this is the hierarchy of browser objects.
Window Object
: Chief browser object which contains properties and methods that we can use to access different objects within the window.
window.navigator
: returns a reference to the navigator object.window.screen
: returns a reference to the screen object associated with the window.window.history
: returns a reference to the history object.window.location
: gets and sets the location/current URL of the window object.window.document
: (can be shortened to document) returns a reference to the document that the window contains.All of the objects above can be shortened to their latter half (i.e. window.document => document)
The Browser Diagram
User interface
: Browser interface that the user interacts with such as the address bar, buttons, bookmarks, etc. Includes everything except for the requested page content.
Browser Engine
: Manages the interactions between the UI and the rendering machine.
Rendering Engine
: Displays and renders the requested page content.
Networking
: Handles network calls, such as HTTP requests.
Javascript Interpreter
: Parses and executes JS code.
UI Backend
: Used for drawing basic widgets like combo boxes and windows; uses operating system user interface methods.
Data Storage
: The persistence of data stored in the browser, such as cookies.
Browsing the web is just a series of requests
and responses
.
HTTP Request
, where we make a request to the server to fetch a URL.The Request Response Cycle Diagram
client
side, the middle is the internet
, and right side is the server
side.The Browser’s Role in the Request-Response Cycle
The Browser also does other things besides letting users make requests.
The Network Tab
in your Developer's Tools
will allow you to view all the requests and responses.
Using the Window API
// windowTest.js
// Open a new window
newWindow = window.open("", "", "width=100, height=100");
// Resize the new window
newWindow.resizeTo(500, 500);
Context, Scope, and Anonymous Function
Execution Context
refers more to scope than to context.
Creation Phase
: The interpreter creates a variable object(activation object) - the scope chain is then initialized.Excution Phase
: Code is interpreted and executed.IIFE’s are a type of closure.
The important things to note about context are:
owns
the function.Running a script on DOMContentLoaded
window.addEventListener("DOMContentLoaded", (event) => {
console.log("This script loaded when the DOM was ready.");
});
Running a script on page load The alternative to DOMContentLoaded is, waiting for everything to load in the document before we run our script. For this we can use window.onload
window.onload = () => {
console.log(
"This script loaded when all the resources and the DOM were ready."
);
};
Ways to prevent a script from running until page loads Some methods include:
<script async src="scriptA.js"></script>
<script defer src="scriptB.js"></script>
<script async defer src="scriptC.js"></script>