Table of contents
How Browsers Work
Browsers are complex applications that do much more than just displaying HTML pages. They are essentially a full-fledged operating system with many components working together to load and render web pages.
When you load a web page in your browser, several things happen behind the scenes:
The browser loads the HTML file. It converts the raw bytes into characters using the specified encoding, usually UTF-8. It then tokenizes the HTML into elements like <h1>, <p>, etc. It creates DOM objects and establishes relationships between them. This results in the DOM tree.
The browser also loads any CSS files referenced in the HTML. It follows a similar process of converting bytes to characters, tokenizing into selectors, creating style objects and building the CSSOM (CSS Object Model).
The browser's rendering engine combines the DOM and CSSOM to build the render tree. This involves applying styles, calculating layout and positioning of elements.
As the browser encounters <script> tags, it will pause the rendering process, load and execute the JavaScript. The JavaScript can then modify the DOM.
The browser then starts painting the render tree to the screen, displaying the finished webpage.
The browser also handles user interactions, network requests, and JavaScript execution to provide an interactive experience.
The rendering engine, written in C++, is responsible for taking the HTML, CSS and JavaScript and converting it into low-level commands for displaying and interacting with the webpage. The rendering process is complex but ultimately results in the webpage we see.
Hope this overview helps explain at a high level how browsers work! Let me know if you have any other questions.