Junior
What happens when a page loads?
sobes.tech AI
Answer from AI
- The user enters a URL in the browser's address bar or clicks a link.
- The browser performs a DNS lookup to find the IP address of the host.
- The browser establishes a TCP connection with the web server using the obtained IP address.
- The browser sends an HTTP request to the server, requesting the HTML document.
- The server processes the request and sends an HTTP response with the HTML content and metadata (headers).
- The browser receives the HTML document and begins parsing it, building the Document Object Model (DOM).
- When it encounters links to CSS files, JavaScript files, and images in the HTML, the browser makes additional HTTP requests to load them.
- The browser processes CSS, applying styles to DOM elements, creating the CSS Object Model (CSSOM).
- After building the DOM and CSSOM, the browser combines them to create the Render Tree.
- The browser performs layout calculations (Layout/Reflow) to determine the geometric characteristics of elements.
- The browser paints the elements on the screen based on the Render Tree and layout information.
- The browser executes JavaScript code, which may modify the DOM, CSSOM, or initiate additional HTTP requests (e.g., AJAX). Changes may trigger a re-execution of steps 6-11.
// Example of a simple HTTP server in Node.js
const http = require('http');
const server = http.createServer((req, res) => {
// Handling incoming request
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Hello, world!</h1>'); // Sending HTML response
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
| Stage | Description |
|---|---|
| DNS Lookup | Converting domain name to IP address |
| TCP Connection | Establishing a connection between client and server |
| HTTP Request | Request from client to server |
| HTTP Response | Response from server to client |
| Parsing & DOM | Building the DOM tree from HTML |
| CSSOM | Building the CSSOM tree from CSS |
| Render Tree | Combining DOM and CSSOM |
| Layout/Reflow | Calculating positions and sizes of elements |
| Paint | Rendering elements on the screen |
| JavaScript Exec | Executing scripts |