-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zh-cn: translate URL.canParse()
static method
#22215
Conversation
Preview URLs Flaws (2)URL:
External URLs (1)URL:
(comment last updated: 2024-07-10 13:06:06) |
> **备注:** `url` 和 `base` 参数将根据你传递的任何值进行字符串化,就像其他接受字符串的 Web API 一样。 | ||
> 特别是,你可以使用一个现有的 {{domxref("URL")}} 对象作为任一参数,它将被字符串化为该对象的 {{domxref("URL.href", "href")}} 属性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> **备注:** `url` 和 `base` 参数将根据你传递的任何值进行字符串化,就像其他接受字符串的 Web API 一样。 | |
> 特别是,你可以使用一个现有的 {{domxref("URL")}} 对象作为任一参数,它将被字符串化为该对象的 {{domxref("URL.href", "href")}} 属性。 | |
> **备注:** `url` 和 `base` 参数将根据你传递的任何值进行字符串化,就像其他接受字符串的 Web API 一样。特别是,你可以使用一个现有的 {{domxref("URL")}} 对象作为任一参数,它将被字符串化为该对象的 {{domxref("URL.href", "href")}} 属性。 |
- `url` | ||
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对地址。 | ||
如果 `url` 是相对地址,则 `base` 为必填项,并将用作基本地址。 | ||
如果 `url` 是绝对地址,给定的 `base` 将被忽略。 | ||
- `base` {{optional_inline}} | ||
- : 字符串,表示在 `url` 是相对 URL 的情况下使用的基本地址。 | ||
如果未指定,则默认为 `undefined`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `url` | |
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对地址。 | |
如果 `url` 是相对地址,则 `base` 为必填项,并将用作基本地址。 | |
如果 `url` 是绝对地址,给定的 `base` 将被忽略。 | |
- `base` {{optional_inline}} | |
- : 字符串,表示在 `url` 是相对 URL 的情况下使用的基本地址。 | |
如果未指定,则默认为 `undefined`。 | |
- `url` | |
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对地址。如果 `url` 是相对地址,则 `base` 为必填项,并将用作基本地址。如果 `url` 是绝对地址,给定的 `base` 将被忽略。 | |
- `base` {{optional_inline}} | |
- : 字符串,表示在 `url` 是相对 URL 的情况下使用的基本地址。如果未指定,则默认为 `undefined`。 |
段落不换行
接下来,我们使用 `URL` 中的 `canParse` 条件检查是否支持 `URL.canParse()` 方法。 | ||
如果支持该方法,我们会记录检查绝对地址、无基本地址的相对地址和有效基本地址的相对地址的结果。 | ||
我们还记录了不支持 `URL.canParse()` 的情况。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
接下来,我们使用 `URL` 中的 `canParse` 条件检查是否支持 `URL.canParse()` 方法。 | |
如果支持该方法,我们会记录检查绝对地址、无基本地址的相对地址和有效基本地址的相对地址的结果。 | |
我们还记录了不支持 `URL.canParse()` 的情况。 | |
接下来,我们使用 `URL` 中的 `canParse` 条件检查是否支持 `URL.canParse()` 方法。如果支持该方法,我们会记录检查绝对地址、无基本地址的相对地址和有效基本地址的相对地址的结果。我们还记录了不支持 `URL.canParse()` 的情况。 |
最后,下面的代码表明,`baseUrl` 不一定是字符串。 | ||
这里我们传递的是一个 `URL` 对象。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最后,下面的代码表明,`baseUrl` 不一定是字符串。 | |
这里我们传递的是一个 `URL` 对象。 | |
最后,下面的代码表明,`baseUrl` 不一定是字符串。这里我们传递的是一个 `URL` 对象。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
���以在 Files changed 页面一次性应用反馈的建议
title: URL:canParse() 静态方法 | ||
slug: Web/API/URL/canParse_static | ||
l10n: | ||
sourceCommit: 7c7a078585a36eef960de1d93d28b20691486b99 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是对应文档的最后一次��交的 commit-sha
sourceCommit: 7c7a078585a36eef960de1d93d28b20691486b99 | |
sourceCommit: 1145d9748b094cf9bfd42b7ea8847ce8f647f002 |
|
||
{{domxref("URL")}} 接口的 **`URL.canParse()`** 静态方法返回一个布尔值,表示绝对地址或与基本地址结合的相对地址是否可解析和有效。 | ||
|
||
这是在 [try...catch](/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch) 代码块中构造 `URL` 的快速而简单的替代方法。对于与 [URL() 构造函数](/zh-CN/docs/Web/API/URL/URL)返回成功的值相同,它将返回 `true`,而对于会导致构造函数抛出的错误的值,它返回 `false`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是在 [try...catch](/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch) 代码块中构造 `URL` 的快速而简单的替代方法。对于与 [URL() 构造函数](/zh-CN/docs/Web/API/URL/URL)返回成功的值相同,它将返回 `true`,而对于会导致构造函数抛出的错误的值,它返回 `false`。 | |
这是在 [try...catch](/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch) 代码块中构造 `URL` 的快速而简单的替代方法。对于会��得 [`URL()` 构造函数](/zh-CN/docs/Web/API/URL/URL)成功的相同值,它将返回 `true`,而对于会导致构造函数抛出的错误的值,它返回 `false`。 |
### 参数 | ||
|
||
- `url` | ||
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对地址。如果 `url` 是相对地址,则 `base` 为必填项,并将用作基本地址。如果 `url` 是绝对地址,给定的 `base` 将被忽略。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对地址。如果 `url` 是相对地址,则 `base` 为必填项,并将用作基本地址。如果 `url` 是绝对地址,给定的 `base` 将被忽略。 | |
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对 URL。如果 `url` 是相对 URL,则 `base` 为必填项,并将用作基本 URL。如果 `url` 是绝对 URL,给定的 `base` 将被忽略。 |
- `url` | ||
- : 字符串或其他任何带有 {{Glossary("stringifier")}} 的对象(例如,包括 {{htmlelement("a")}} 或 {{htmlelement("area")}} 元素),表示绝对或相对地址。如果 `url` 是相对地址,则 `base` 为必填项,并将用作基本地址。如果 `url` 是绝对地址,给定的 `base` 将被忽略。 | ||
- `base` {{optional_inline}} | ||
- : 字符串,表示在 `url` 是相对 URL 的情况下使用的基本地址。如果未指定,则默认为 `undefined`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- : 字符串,表示在 `url` 是相对 URL 的情况下使用的基本地址。如果未指定,则默认为 `undefined`。 | |
- : 字符串,表示在 `url` 是相对 URL 的情况下使用的基本 URL。如果未指定,则默认为 `undefined`。 |
|
||
## 示例 | ||
|
||
本示例演示了如何使用 `URL.canParse()` 静态方法来处理一些不同的绝对地址和相对地址的值。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本示例演示了如何使用 `URL.canParse()` 静态方法来处理一些不同的绝对地址和相对地址的值。 | |
本示例演示了如何使用 `URL.canParse()` 静态方法来处理一些不同的绝对和相对 URL 的值。 |
result = URL.canParse(url); | ||
log(` URL.canParse("${url}"): ${result}`); | ||
|
||
log("\nTest relative URL with valid base URL"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log("\nTest relative URL with valid base URL"); | |
log("\n测试具有有效基本 URL 的相对 URL"); |
result = URL.canParse(url, baseUrl); | ||
log(` URL.canParse("${url}","${baseUrl}"): ${result}`); | ||
} else { | ||
log("URL.canParse() not supported"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log("URL.canParse() not supported"); | |
log("不支持 URL.canParse()"); |
|
||
```js | ||
if ("canParse" in URL) { | ||
log("\nTest relative URL with base URL supplied as a URL object"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log("\nTest relative URL with base URL supplied as a URL object"); | |
log("\n测试以 URL 对象为基本 URL 的相对 URL"); |
|
||
各项检查结果如下。 | ||
|
||
{{EmbedLiveSample('Examples', '100%', '200')}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{{EmbedLiveSample('Examples', '100%', '200')}} | |
{{EmbedLiveSample('示例', '100%', '200')}} |
## 参见 | ||
|
||
- {{domxref("URL.URL", "URL()")}} | ||
- [`URL.canParse()` 的 polyfill](https://github.com/zloirock/core-js#url-and-urlsearchparams) 可在 [`core-js`](https://github.com/zloirock/core-js) 中使用 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- [`URL.canParse()` 的 polyfill](https://github.com/zloirock/core-js#url-and-urlsearchparams) 可在 [`core-js`](https://github.com/zloirock/core-js) 中使用 | |
- [`core-js`](https://github.com/zloirock/core-js) 中可用的 [`URL.canParse()` 的 polyfill](https://github.com/zloirock/core-js#url-and-urlsearchparams) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for reviewing my code!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @JinMokai
Description
Translate the canParse_static method page in the url page
Motivation
Do something open source that makes sense, just this