Skip to content

URL

理解 URLs — 地址那个 locate 每个 resource on web, and 什么每个部分平均值.

你将学到

  • 什么 URL 是 and 什么它 点到
  • anatomy 的 URL: scheme, host, path, 查询, 片段
  • 如何查询参数通过数据到 服务器
  • 如何 URL encoding handles 特殊 characters

概念讲解

URL

A URL (Uniform Resource Locator) is the address of a resource on the web. Every web page, image, and API endpoint has a URL. When you type one in the browser, it knows exactly where to send the HTTP request.

Anatomy of a URL

https://api.example.com:443/users/42?sort=name&page=2#settings

| Part | Example | Meaning | |------|---------|---------| | Scheme | HTTPS | The protocol (http, https, ftp) | | Host | API.示例.com | The server's domain name | | Port | :443 | The connection port (often omitted) | | Path | /users/42 | The specific resource on the server | | Query | ?排序=name&page=2 | Key-value parameters | | Fragment | #设置 | An anchor within the page |

Query Parameters

The query string (after ?) passes data to the server as key-value pairs. Multiple parameters are joined with &:

/search?q=javascript&limit=10&lang=en

Servers read these to filter, sort, or paginate results. They are visible in the URL, so never put secrets there.

URL Encoding

URLs can only contain certain characters. Spaces, non-ASCII letters, and symbols must be percent-encoded — replaced with a % followed by their hex code. A space becomes %20; a Chinese character becomes several %XX sequences.

https://example.com/search?q=hello%20world

This is why URLs sometimes look ugly with lots of % signs. The browser and server handle encoding automatically, but you should understand it when building URLs by hand.

URLs vs. URIs vs. URNs

Strictly, a URL *locates* (tells you where), a URN *names* (tells you what), and a URI is the umbrella term. In everyday web development, "URL" is what you will use.

示例代码

              // Building and parsing URLs with the built-in URL API
const url = new URL("https://api.example.com/users");
url.searchParams.set("sort", "name");
url.searchParams.set("page", "2");
url.searchParams.set("q", "hello world");  // auto-encoded!


console.log(url.toString());
// https://api.example.com/users?sort=name&page=2&q=hello+world

console.log(url.searchParams.get("q"));   // "hello world" (decoded)
console.log(url.host);      // "api.example.com"
console.log(url.pathname);  // "/users"
            

URL API handles encoding and parsing for 你. searchParams.设置 encodes automatically; searchParams.获取 decodes. 从不构建查询字符串通过 hand — use 这个 API.

动手试一试

  • URL Encoder

    百分比-encode and decode URLs — see 精确地如何特殊 characters 是 represented.

常见错误

错误写法

构建查询字符串通过 concatenating 字符串不使用 encoding.

正确做法

Use URL and URLSearchParams APIs. 它们 encode automatically. Hand-构建 URLs leads 到 bugs 使用空间, ampersands, and 非-ASCII characters.

错误写法

Putting sensitive 数据 (API 键, 令牌) in URL 查询字符串.

正确做法

URLs 是 logged 通过服务器, proxies, and 浏览器 history. Put secrets in HTTP 请求头 or 请求 bodies, 从不 in URL.

相关工具

练习 URL

4 个练习

练习 URL

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误