> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 获取 URL 参数

## 基础写法

**获取 url 链接中指定的参数值：**

```javascript title="代码示例"
// 获取地址栏参数值
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
        r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
```

:::tip 方法介绍

- location.search：**search** 属性是一个可读可写的字符串，可设置或返回当前 **URL** 的查询部分，即 **?** 之后部分(包含字符串)；
- **unescape(string)**：unescape() 函数可对通过 escape() 编码的字符串进行解码；通过找到形式为 %xx 和 %uxxxx 的字符序列（x 表示十六进制的数字），用 Unicode 字符 \u00xx 和 \uxxxx 替换这样的字符序列进行解码。

:::

## 简化写法

**使用三目运算符简化代码：**

```javascript title="代码示例"
getQueryStr(n) {
  let r, reg = new RegExp("(^|&)" + n + "=([^&]*)(&|$)");
  r = window.location.search.substr(1).match(reg);
  return r ? decodeURI(r[2]) : null;
}
```

:::warning 注意
ECMAScript v3 已从标准中删除了 **unescape()** 函数，并反对使用它，因此应该用 **decodeURI(string)** 和 **decodeURIComponent(string)** 取而代之。
:::
