Skip to content

浏览器

理解什么 web 浏览器做 — 渲染, JavaScript execution, 缓存, and storage.

你将学到

  • 什么浏览器是 and 它的核心作业
  • 如何渲染 turns HTML/CSS 到像素
  • 如何浏览器缓存 resources for 速度
  • 什么 localStorage and Cookie 是 for

概念讲解

Browser

A web browser (Chrome, Firefox, Safari, Edge) is the application that fetches, interprets, and displays web pages. It is the client in the client–server model. To a user it is a window to the web; to a developer it is a runtime, a renderer, and a toolkit.

The Browser's Core Jobs

  1. Fetch — request HTML, CSS, JS, and images from servers.
  2. Parse — read the HTML and build the DOM; read the CSS and build the CSSOM.
  3. Render — combine the DOM and CSSOM into a render tree, lay out elements, and paint pixels on screen.
  4. Execute — run JavaScript via its built-in engine (V8 in Chrome, SpiderMonkey in Firefox).
  5. Respond — fire events when the user clicks, types, or scrolls.

Caching

Browsers cache (save) resources so they do not have to re-download them on your next visit. That is why a site loads faster the second time. As a developer, you control caching with HTTP headers (缓存-Control, ETag). Getting caching right is a major part of web performance.

Storage

Browsers offer several ways to store data on the user's device:

  • Cookies — small key-value pairs sent with every request. Used for sessions and tracking.
  • localStorage — larger, stays until cleared, never sent to the server. Great for user preferences.
  • sessionStorage — like localStorage but cleared when the tab closes.
  • IndexedDB — a database for large, structured data.

Why Developers Care

The browser is your execution environment. Knowing how it renders, caches, and stores helps you build faster, more reliable sites. The browser's built-in DevTools (covered later in this topic) are your primary window into what the browser is doing.

示例代码

              // Using localStorage to remember a user's theme choice
function applyTheme(theme) {
  document.body.className = theme;
  localStorage.setItem("theme", theme);   // persist
}

// On page load, restore the saved theme
const savedTheme = localStorage.getItem("theme") || "light";
applyTheme(savedTheme);

// A button to toggle the theme
document.getElementById("toggle").addEventListener("click", () => {
  const current = localStorage.getItem("theme");
  applyTheme(current === "light" ? "dark" : "light");
});
            

localStorage 保存主题所以它 survives page reloads and 浏览器 restarts. Unlike Cookie, 它是 从不发送到 服务器 — 它是 purely 客户端-边状态.

动手试一试

  • JWT Decoder

    浏览器存储 JWTs in localStorage or Cookie. Decode one 到 see payload 浏览器会 carry.

常见错误

错误写法

存储 sensitive 数据 (密码, 令牌) in localStorage.

正确做法

localStorage 是 readable 通过任何 JavaScript on page, 所以它 是 vulnerable 到 XSS. Use httpOnly Cookie for sensitive 会话数据; reserve localStorage for 非-sensitive preferences.

错误写法

Assuming 每个用户有 latest 浏览器.

正确做法

检查 feature support on caniuse.com and provide fallbacks. Older 浏览器可能 lack modern APIs. Tools 如 Babel transpile 你的 code for broader compatibility.

相关速查表

练习 浏览器

3 个练习

练习 浏览器

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误